home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / src / ddpro.c < prev    next >
Text File  |  1997-08-08  |  65KB  |  3,132 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: ddpro.c,v 1.24 1997/06/27 18:03:30 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    ddpro.c
  35.  *
  36.  *    Entry points for messages from network.
  37.  *
  38. $Log: ddpro.c,v $
  39.  * Revision 1.24  1997/06/27  18:03:30  pvmsrc
  40.  * Integrated WIN32 changes.
  41.  *
  42.  * Revision 1.23  1997/06/17  19:04:26  pvmsrc
  43.  * Enhanced WT_SPAWN case in hostfailentry().
  44.  *     - for PvmTaskDefault, if the host that the pvmd picked has failed,
  45.  *         go ahead and retry spawning it on another host.
  46.  *
  47.  * Revision 1.22  1997/06/16  13:39:44  pvmsrc
  48.  * Updated exectasks() to pass additional information during spawn ops.
  49.  *
  50.  * Revision 1.21  1997/05/19  21:18:13  pvmsrc
  51.  * Awwww Shit.  I did it.
  52.  *     - crunchzap() sux, can't free buf in startack() until ac/av used.
  53.  *
  54.  * Revision 1.20  1997/05/12  20:28:17  pvmsrc
  55.  * Removed duplicate #includes...
  56.  *
  57.  * Revision 1.19  1997/05/07  21:22:07  pvmsrc
  58.  * AAAIIIEEEEEEEEE!!!!
  59.  *     - removed all static-limited string unpacking:
  60.  *         * replaced with use of:
  61.  *             upkstralloc() / PVM_FREE() (for pvmd stuff).
  62.  *             new pvmupkstralloc() / PVM_FREE() (for lpvm stuff).
  63.  *         * manual allocation of local buffers for sprintf() & packing.
  64.  *         * alternate static string arrays to replace fixed-length cases.
  65.  *         * I hope this shit works...  :-Q
  66.  *
  67.  * Revision 1.18  1997/05/02  13:43:43  pvmsrc
  68.  *     call mpp_load for SP2MPI
  69.  *
  70.  * Revision 1.17  1997/04/30  21:25:54  pvmsrc
  71.  * SGI Compiler Warning Cleanup.
  72.  *
  73.  * Revision 1.16  1997/04/10  18:59:06  pvmsrc
  74.  * Damn.  Stupid typo (as opposed to a smart one?).
  75.  *
  76.  * Revision 1.15  1997/04/10  18:46:56  pvmsrc
  77.  * Bug fix in dm_db():
  78.  *     - return value for mb_lookup() call was overwriting req index,
  79.  *         need to save req in case PvmMboxWaitForInfo...
  80.  *
  81.  * Revision 1.14  1997/04/10  18:32:03  pvmsrc
  82.  * Damn.  Forgot the stupid PvmMboxWaitForInfo flag check...  D-Oh!
  83.  *
  84.  * Revision 1.13  1997/04/10  17:53:19  pvmsrc
  85.  * Added handling of blocking (PvmMboxWaitForInfo) calls to pvm_recvinfo().
  86.  *     - in dm_db(), on TMDB_GET, if not found create WT_RECVINFO.
  87.  *     - in dm_db(), on TMDB_PUT, check for matching WT_RECVINFO & release.
  88.  *     - in hostfailentry(), wipe out any pending WT_RECVINFOs if host
  89.  *         where task resided is now toast.
  90.  *
  91.  * Revision 1.12  1997/04/09  19:37:56  pvmsrc
  92.  * Added class name and index args to pvmreset() routine:
  93.  *     - allow systematic elimination of leftover persistents...
  94.  *     - heh, heh, heh...
  95.  * In dm_db(), check for name (class) and req (index) match
  96.  *     for TMDB_RESET case (""/-1 for all, resp).
  97.  *
  98.  * Revision 1.11  1997/04/09  14:36:06  pvmsrc
  99.  * PVM patches from the base 3.3.10 to 3.3.11 versions where applicable.
  100.  * Originals by Bob Manchek. Altered by Graham Fagg where required.
  101.  *
  102.  * Revision 1.10  1997/04/08  19:57:47  pvmsrc
  103.  * Promoted mbox static "classes" to public global "pvmmboxclasses".
  104.  *     - so pvmd can spank mboxes in ddpro.c...  :-Q
  105.  *     - renamed everywhere, moved decl / extern to global.[ch].
  106.  *
  107.  * Revision 1.9  1997/04/08  19:42:55  pvmsrc
  108.  * *** Added new system reset protocol / wait linkage.
  109.  *     - new DM_RESET / dm_reset() & DM_RESETACK / dm_resetack().
  110.  *     - new WT_RESET wait type.
  111.  *     - modified dm_db() to include new TMDB_RESET(nnr, noresets) option:
  112.  *         * clean up mboxes, except for "no-reset" tasks.
  113.  *         * for persistent mboxes, set up WT_RESET to remove mbox on task
  114.  *             exit, propagate to task's host via DM_RESET.
  115.  *         * on task exit, WT_RESET wipes mbox out, DM_RESETACK passes
  116.  *             word on to master pvmd (if necessary).
  117.  *
  118.  * Revision 1.8  1997/03/07  14:05:11  pvmsrc
  119.  * Phixed Phunky Phil #endif.
  120.  *     - no preceding tab on some archs.
  121.  *
  122.  * Revision 1.7  1997/03/06  20:55:34  pvmsrc
  123.  *     add call to mpp_load when loading onto compute partition.
  124.  *
  125.  * Revision 1.6  1997/02/13  19:05:05  pvmsrc
  126.  * Fixed mbox cleanup problem:
  127.  *     - in dm_db() for TMDB_PUT case, if successful create master PVMD
  128.  *         notify on inserting task (if task not local, forward DM_NOTIFY).
  129.  *     - then on task exit, call mb_tidy() if WT_TASKX notify wait context
  130.  *         exists (in hostfailentry() and task_cleanup()), or if empty
  131.  *         notify propagates back to master PVMD via DM_NOTIFYACK.
  132.  *
  133.  * Revision 1.5  1997/02/13  15:10:00  pvmsrc
  134.  * Removed unnecessary extern for struct waitc *waitlist.
  135.  *     - now in global.h.
  136.  *
  137.  * Revision 1.4  1997/01/28  19:26:14  pvmsrc
  138.  * New Copyright Notice & Authors.
  139.  *
  140.  * Revision 1.3  1996/10/25  13:57:16  pvmsrc
  141.  * Replaced old #includes for protocol headers:
  142.  *     - <pvmsdpro.h>, "ddpro.h", "tdpro.h"
  143.  * With #include of new combined header:
  144.  *     - <pvmproto.h>
  145.  *
  146.  * Revision 1.2  1996/10/24  21:50:47  pvmsrc
  147.  * Moved #include "global.h" below other #include's for typing.
  148.  * Added #include <pvmtev.h> for TEV_* constants.
  149.  * Added extern struct Pvmtracer pvmtracer for tracer info.
  150.  * Modified exectasks():
  151.  *     - if tracer present, adjust settings for trace & output collect,
  152.  *         and munge the environment to replace values for PVMTMASK,
  153.  *         PVMTRCBUF & PVMTRCOPT.
  154.  *     - replaced inline code with call to new tev_send_newtask().
  155.  *     - modified checking of trctid / outtid, check for < 0, not non-zero
  156.  *         in case task denies external collect.
  157.  * Added handling of new DM_SLCONF_TRACE in dm_slconf():
  158.  *     - tracer update configuration msg.
  159.  *     - also added packing up of same in startack().
  160.  *
  161.  * Revision 1.1  1996/09/23  23:44:06  pvmsrc
  162.  * Initial revision
  163.  *
  164.  * Revision 1.15  1996/05/13  20:23:21  manchek
  165.  * a few fixes to manual startup code
  166.  *
  167.  * Revision 1.14  1995/07/24  18:23:14  manchek
  168.  * spelling problem
  169.  *
  170.  * Revision 1.13  1995/07/19  21:27:20  manchek
  171.  * use new function pvmnametag instead of [dts]mname
  172.  *
  173.  * Revision 1.12  1995/07/03  19:03:35  manchek
  174.  * if shared memory, get status from global table in dm_task before replying
  175.  *
  176.  * Revision 1.11  1995/06/16  16:11:34  manchek
  177.  * hack to pass output and trace sink to mpp_load for PGON
  178.  *
  179.  * Revision 1.10  1995/05/30  17:26:30  manchek
  180.  * added ifdefs for SP2MPI architecture
  181.  *
  182.  * Revision 1.9  1995/05/17  16:04:36  manchek
  183.  * added HF_OVERLOAD flag.
  184.  * changed global mytid to pvmmytid
  185.  *
  186.  * Revision 1.8  1995/02/01  21:03:07  manchek
  187.  * addhosts returns PvmDSysErr instead of -1 if something breaks.
  188.  * mpp_load is called with environment so it can be passed to tasks.
  189.  * dm_execack processes received tids in order instead of backwards
  190.  *
  191.  * Revision 1.7  1994/10/15  19:02:36  manchek
  192.  * cast message tags for comparison as integer.
  193.  * send output and trace open messages for dmp and shmem ports.
  194.  * check newhosts when deleting host
  195.  *
  196.  * Revision 1.6  1994/07/18  19:18:10  manchek
  197.  * hostfailentry() no longer matches pvmd' to t0.
  198.  * wa_dep wasn't propogated from hoststart to htupd
  199.  *
  200.  * Revision 1.5  1994/06/30  21:33:46  manchek
  201.  * addhosts() uses PVM_DPATH envar
  202.  *
  203.  * Revision 1.4  1994/06/21  18:29:21  manchek
  204.  * subscript arith in dmname() broke with opt
  205.  *
  206.  * Revision 1.3  1994/06/04  22:33:16  manchek
  207.  * missed resetting busyadding in hostfailentry(WT_HTUPD)
  208.  *
  209.  * Revision 1.2  1994/06/03  20:38:12  manchek
  210.  * version 3.3.0
  211.  *
  212.  * Revision 1.1  1993/08/30  23:26:47  manchek
  213.  * Initial revision
  214.  *
  215.  */
  216.  
  217.  
  218. #ifdef NEEDMENDIAN
  219. #include <machine/endian.h>
  220. #endif
  221. #ifdef NEEDENDIAN
  222. #include <endian.h>
  223. #endif
  224. #ifdef NEEDSENDIAN
  225. #include <sys/endian.h>
  226. #endif
  227. #ifndef WIN32
  228. #include <rpc/types.h>
  229. #include <rpc/xdr.h>
  230. #include <sys/time.h>
  231. #include <sys/socket.h>
  232. #include <netinet/in.h>
  233. #include <netdb.h>
  234. #else 
  235. #include "pvmwin.h"
  236. #include <sys/stat.h>
  237. #include <time.h>
  238. #include <direct.h>
  239. #include <errno.h>
  240. #endif
  241. #include <sys/types.h>
  242. #ifdef    SYSVSTR
  243. #include <string.h>
  244. #else
  245. #include <strings.h>
  246. #endif
  247. #include <errno.h>
  248. #include <stdio.h>
  249.  
  250. #include <pvm3.h>
  251. #include <pvmproto.h>
  252. #include "pvmalloc.h"
  253. #include "host.h"
  254. #include "pmsg.h"
  255. #include "waitc.h"
  256. #include "task.h"
  257. #include "listmac.h"
  258. #if defined(IMA_PGON) || defined(IMA_I860) || defined(IMA_CM5) || defined(SHMEM) || defined(IMA_SP2MPI)
  259. #include "pvmdmp.h"
  260. #endif
  261. #include "bfunc.h"
  262. #include <pvmtev.h>
  263. #include "msgbox.h"
  264. #include "global.h"
  265.  
  266.  
  267. void pvmbailout();
  268. char *inadport_hex();
  269. char *getenv();
  270. void hex_inadport __ProtoGlarp__ (( char *, struct sockaddr_in * ));
  271. void mesg_rewind __ProtoGlarp__ (( struct pmsg * ));
  272. void tev_send_newtask();
  273.  
  274. char **colonsep();
  275. char *varsub();
  276. char *pvmnametag();
  277.  
  278. extern int pvmdebmask;                /* from pvmd.c */
  279. extern char **epaths;                /* from pvmd.c */
  280. extern char *debugger;                /* from pvmd.c */
  281. extern struct htab *filehosts;        /* from pvmd.c */
  282. extern struct htab *hosts;            /* from pvmd.c */
  283. extern int hostertid;                /* from pvmd.c */
  284. extern struct task *locltasks;        /* from task.c */
  285. extern char *myarchname;            /* from pvmd.c */
  286. extern int myhostpart;                /* from pvmd.c */
  287. extern int pvmmytid;                /* from pvmd.c */
  288. extern struct htab *newhosts;        /* from pvmd.c */
  289. extern int pprime;                    /* from pvmd.c */
  290. extern int runstate;                /* from pvmd.c */
  291. extern int pvmschedtid;                /* from pvmd.c */
  292. extern struct Pvmtracer pvmtracer;    /* from pvmd.c */
  293. extern int tidhmask;                /* from pvmd.c */
  294. extern int tidlmask;                /* from pvmd.c */
  295.  
  296. int busyadding = 0;                    /* lock for addhost op */
  297. int hosterwid = 0;                    /* wid waiting on hoster */
  298.  
  299.  
  300. /***************
  301.  **  Private  **
  302.  **           **
  303.  ***************/
  304.  
  305. int dm_add();
  306. int dm_addack();
  307. int dm_db();
  308. int dm_dback();
  309. int dm_delhost();
  310. int dm_delhostack();
  311. int dm_halt();
  312. int dm_hostsync();
  313. int dm_hostsyncack();
  314. int dm_htcommit();
  315. int dm_htdel();
  316. int dm_htupd();
  317. int dm_htupdack();
  318. int dm_mca();
  319. int dm_notify();
  320. int dm_notifyack();
  321. int dm_null();
  322. int dm_pstat();
  323. int dm_pstatack();
  324. int dm_reset();
  325. int dm_resetack();
  326. int dm_sendsig();
  327. int dm_slconf();
  328. int dm_exec();
  329. int dm_execack();
  330. int dm_startack();
  331. int dm_task();
  332. int dm_taskack();
  333. int dm_taskout();
  334.  
  335. int (*netswitch[])() = {
  336.     dm_add, dm_addack,
  337.     dm_exec, dm_execack,
  338.     dm_sendsig,
  339.     dm_htupd, dm_htupdack,
  340.     dm_htcommit,
  341.     dm_slconf,
  342.     dm_startack,
  343.     dm_task, dm_taskack,
  344.     dm_delhost, dm_delhostack,
  345.     dm_null,
  346.     dm_taskout,
  347.     dm_pstat, dm_pstatack,
  348.     dm_halt,
  349.     dm_mca,
  350.     dm_notify, dm_notifyack,
  351.     dm_db, dm_dback,
  352.     dm_reset, dm_resetack,
  353.     dm_htdel,
  354.     dm_hostsync, dm_hostsyncack,
  355. };
  356.  
  357.  
  358. /*    hostfailentry()
  359. *
  360. *    We've decided a remote pvmd has failed.
  361. *    Wake up any wait context waiting on that host (different action
  362. *    for each kind of wait).
  363. *    Send a HTDEL message to remaining hosts except us.
  364. */
  365.  
  366. int
  367. hostfailentry(hp)
  368.     struct hostd *hp;
  369. {
  370.     int hpart = hp->hd_hostpart;
  371.     struct waitc *wp, *wp2;
  372.     struct pmsg *mp;
  373.     struct pvmmentry *ep;
  374.  
  375.     if (pvmdebmask & PDMHOST) {
  376.         pvmlogprintf("hostfailentry() host %s\n", hp->hd_name);
  377.         hd_dump(hp);
  378.     }
  379.  
  380.     if (hp == hosts->ht_hosts[hosts->ht_master]) {
  381.         pvmlogerror("hostfailentry() lost master host, we're screwwwed\n");
  382.         pvmbailout(0);
  383.     }
  384.  
  385.     /*
  386.     * if we're master pvmd, send HT_DEL message to all others
  387.     */
  388.  
  389.     if (hp->hd_hostpart && hosts->ht_master == hosts->ht_local) {
  390.         struct hostd *hp2;
  391.         int hh;
  392.  
  393.         mp = mesg_new(0);
  394.         mp->m_tag = DM_HTDEL;
  395.         pkint(mp, hosts->ht_serial);
  396.         pkint(mp, hp->hd_hostpart);
  397.         for (hh = hosts->ht_last; hh > 0; hh--)
  398.             if (hh != hosts->ht_local
  399.             && (hp2 = hosts->ht_hosts[hh]) && hp2 != hp) {
  400.                 mp->m_ref++;
  401.                 mp->m_dst = hp2->hd_hostpart | TIDPVMD;
  402.                 sendmessage(mp);
  403.             }
  404.         pmsg_unref(mp);
  405.  
  406.         /* inform the scheduler too */
  407.  
  408.         if (pvmschedtid) {
  409.             mp = mesg_new(0);
  410.             mp->m_tag = SM_HOSTX;
  411.             mp->m_dst = pvmschedtid;
  412.             pkint(mp, hp->hd_hostpart | TIDPVMD);
  413.             sendmessage(mp);
  414.         }
  415.     }
  416.  
  417.     for (wp = waitlist->wa_link; wp != waitlist; wp = wp->wa_link) {
  418.         if (wp->wa_on && (wp->wa_on & TIDHOST) == hpart) {
  419.             switch (wp->wa_kind) {
  420.  
  421.             case WT_ADDHOST:    /* the master must have died */
  422.             case WT_DELHOST:    /* the master must have died */
  423.  
  424.                 pvmlogprintf("hostfailentry() can't deal with wait kind %d\n",
  425.                         wp->wa_kind);
  426.                 break;
  427.  
  428.             case WT_HTUPD:
  429.                 if (wp->wa_peer == wp) {
  430.                     int hh;
  431.  
  432.                     mp = mesg_new(0);
  433.                     mp->m_tag = DM_HTCOMMIT;
  434.  
  435.                     for (hh = hosts->ht_last; hh > 0; hh--)
  436.                         if (hh != hosts->ht_local && (hp = hosts->ht_hosts[hh])) {
  437.                             mp->m_ref++;
  438.                             mp->m_dst = hp->hd_hostpart | TIDPVMD;
  439.                             sendmessage(mp);
  440.                         }
  441.                     pmsg_unref(mp);
  442.  
  443.                     busyadding = 0;
  444.                     sendmessage(wp->wa_mesg);
  445.                     wp->wa_mesg = 0;
  446.                 }
  447.                 break;
  448.  
  449.             case WT_SPAWN:
  450.                 {
  451.                     struct waitc_spawn *wxp;
  452.                     int retry = 0;
  453.                     int v;
  454.  
  455.                     wxp = (struct waitc_spawn*)wp->wa_spec;
  456.  
  457.     /* mark tasks assigned to this host as failed */
  458.     /* (or reset if PvmTaskDefault) */
  459.  
  460.                     for (v = wxp->w_veclen; v-- > 0; )
  461.                         if (wxp->w_vec[v] == hp->hd_hostpart)
  462.                             if (!(wxp->w_flags
  463.                                     & (PvmTaskHost|PvmTaskArch)))
  464.                             {
  465.                                 wxp->w_vec[v] = 0;
  466.                                 retry++;
  467.                             }
  468.                             else
  469.                                 wxp->w_vec[v] = PvmHostFail;
  470.  
  471.                     ht_delete(wxp->w_ht, hp);
  472.  
  473.     /* try assigning task again without failed host */
  474.  
  475.                     if ( retry )
  476.                         assign_tasks(wp);
  477.  
  478.     /* is this was the last wait, reply to task */
  479.  
  480.                     if (wp->wa_peer == wp)
  481.                         assign_tasks(wp);
  482.                 }
  483.                 break;
  484.  
  485.             case WT_TASK:
  486.  
  487.     /* send message if we're the last waiter */
  488.  
  489.                 if (wp->wa_peer == wp) {
  490.                     mp = wp->wa_mesg;
  491.                     mp->m_ref++;
  492.                     sendmessage(mp);
  493.                 }
  494.                 break;
  495.  
  496.             case WT_HOSTSTART:
  497.  
  498.     /* reply to waiter */
  499.  
  500.                 busyadding = 0;
  501.                 if (wp->wa_spec) {
  502.                     free_waitc_add((struct waitc_add *)wp->wa_spec);
  503.                     wp->wa_spec = 0;
  504.                 }
  505.                 pkint(wp->wa_mesg, PvmDSysErr);
  506.                 sendmessage(wp->wa_mesg);
  507.                 wp->wa_mesg = 0;
  508.                 break;
  509.  
  510.             case WT_TASKX:
  511.                 if (wp->wa_tid && wp->wa_mesg) {
  512.                     sendmessage(wp->wa_mesg);
  513.                     wp->wa_mesg = 0;
  514.                 }
  515.                 mb_tidy(wp->wa_on);
  516.                 break;
  517.  
  518.             case WT_RESET:
  519.                 if (wp->wa_tid && wp->wa_mesg) {
  520.                     sendmessage(wp->wa_mesg);
  521.                     wp->wa_mesg = 0;
  522.                 }
  523.                 mb_tidy_reset(wp->wa_on);
  524.                 break;
  525.  
  526.             case WT_RECVINFO:
  527.                 /* clean up pending recvinfo */
  528.                 ep = (struct pvmmentry *) wp->wa_spec;
  529.                 if ( ep->me_msg )    /* class name (overload :-Q) */
  530.                     PVM_FREE( ep->me_msg );
  531.                 PVM_FREE( ep );
  532.                 break;
  533.  
  534.             case WT_HOSTF:
  535.                 sendmessage(wp->wa_mesg);
  536.                 wp->wa_mesg = 0;
  537.                 break;
  538.  
  539.             case WT_PSTAT:
  540.             case WT_MSTAT:
  541.             case WT_HOSTSYNC:
  542.                 pkint(wp->wa_mesg, PvmHostFail);
  543.                 sendmessage(wp->wa_mesg);
  544.                 wp->wa_mesg = 0;
  545.                 break;
  546.  
  547.             default:
  548.                 pvmlogprintf("hostfailentry() alien wait kind %d\n",
  549.                         wp->wa_kind);
  550.                 break;
  551.             }
  552.  
  553.             wp2 = wp->wa_rlink;
  554.             wait_delete(wp);
  555.             wp = wp2;
  556.         }
  557.     }
  558.     return 0;
  559. }
  560.  
  561.  
  562. int
  563. netentry(hp, mp)
  564.     struct hostd *hp;
  565.     struct pmsg *mp;
  566. {
  567.     int c = mp->m_tag;
  568.  
  569.     if (pvmdebmask & PDMMESSAGE) {
  570.         pvmlogprintf(
  571.                 "netentry() from host %s src t%x dst t%x tag %s wid %d\n",
  572.                 hp->hd_name, mp->m_src, mp->m_dst, pvmnametag(c, (int *)0),
  573.                 mp->m_wid);
  574. /*
  575.         pvmhdump(mp->m_frag->fr_link->fr_dat, mp->m_frag->fr_link->fr_len,
  576.                 "netentry() ");
  577. */
  578.     }
  579.  
  580.     if (c < (int)DM_FIRST || c > (int)DM_LAST) {
  581.         pvmlogprintf("netentry() message from t%x with bogus code %s\n",
  582.                 mp->m_src, pvmnametag(c, (int *)0));
  583.         goto bail;
  584.     }
  585.  
  586.     c -= DM_FIRST;
  587.     (netswitch[c])(hp, mp);
  588.  
  589. bail:
  590.     pmsg_unref(mp);
  591.     return 0;
  592. }
  593.  
  594.  
  595. /*********************************
  596.  **  Pvmd message entry points  **
  597.  **                             **
  598.  *********************************/
  599.  
  600. /*    hostids_new()
  601. *
  602. *    Get a list of new host tids (ones not it user in host table).
  603. *    Wrap around to lowest number when we reach the maximum.
  604. *    Return as many as are available.
  605. */
  606.  
  607. int
  608. hostids_new(num, tids)
  609.     int *num;            /* count requested, returned */
  610.     int *tids;            /* return tids */
  611. {
  612.     static int lasthh = 1;
  613.     int oldhh;
  614.     int maxhostid = tidhmask >> (ffs(tidhmask) - 1);
  615.     int i;
  616.  
  617.     oldhh = lasthh;
  618.  
  619.     /* find next free hostid */
  620.  
  621.     for (i = 0; i < *num; i++) {
  622.         if (++lasthh > maxhostid)
  623.             lasthh = 1;
  624.         while ((lasthh <= hosts->ht_last && hosts->ht_hosts[lasthh])) {
  625.             if (++lasthh > maxhostid)
  626.                 lasthh = 1;
  627.             if (lasthh == oldhh)
  628.                 goto done;
  629.         }
  630.         tids[i] = lasthh << (ffs(tidhmask) - 1);
  631. /*
  632.         pvmlogprintf("hostids_new() tid %x\n", tids[i]);
  633. */
  634.     }
  635.  
  636. done:
  637.     return *num = i;
  638. }
  639.  
  640.  
  641. free_waitc_add(wxp)
  642.     struct waitc_add *wxp;
  643. {
  644.     int i;
  645.  
  646.     for (i = 0; i < wxp->w_num && wxp->w_hosts[i]; i++)
  647.         hd_unref(wxp->w_hosts[i]);
  648.     PVM_FREE(wxp->w_hosts);
  649.     PVM_FREE(wxp);
  650.     return 0;
  651. }
  652.  
  653.  
  654. /*    addhosts()
  655. *
  656. *    Add hosts requested with DM_ADD or SM_ADD.  Call the hoster or
  657. *    start 'em.
  658. */
  659.  
  660. int
  661. addhosts(mp, rmp)
  662.     struct pmsg *mp;    /* the request message */
  663.     struct pmsg *rmp;    /* reply message blank */
  664. {
  665.     struct hostd *hp, *hp2;
  666.     struct pmsg *mp2;
  667.     struct waitc *wp = 0;
  668.     struct waitc_add *wxp = 0;
  669.     int i, j;
  670.     int count;
  671.     int ngood;
  672.     int ntid;
  673.     struct hostent *he;
  674.     int maxhostid = (tidhmask >> ffs(tidhmask) - 1);
  675.     int hh;
  676.     int pid;
  677.     int *tids;
  678.     char *pvmdpath;
  679.     char *buf;
  680.     int len;
  681.  
  682.     /*
  683.     * have to lock this for 2 reasons:
  684.     *  1. system can't handle overlapping host table updates,
  685.     *  2. the new host tids aren't reserved
  686.     */
  687.     if (busyadding) {
  688. /*
  689.         pvmlogerror("addhosts() already adding new hosts\n");
  690. */
  691.         pkint(rmp, PvmAlready);
  692.         sendmessage(rmp);
  693.         return 0;
  694.     }
  695.  
  696.     busyadding = 1;
  697.  
  698.     /* sanity check count */
  699.  
  700.     if (upkint(mp, &count) || count < 1 || count > maxhostid) {
  701.         pvmlogerror("addhosts() bad msg format\n");
  702.         goto bad;
  703.     }
  704.  
  705.     /*
  706.     * make wait context, extract host list from message,
  707.     */
  708.  
  709.     wp = wait_new(WT_HOSTSTART);
  710.     wp->wa_tid = mp->m_src;
  711.     wp->wa_dep = mp->m_wid;
  712.     wxp = TALLOC(1, struct waitc_add, "waix");
  713.     wxp->w_num = count;
  714.     wxp->w_hosts = TALLOC(count, struct hostd *, "waiv");
  715.     BZERO((char*)wxp->w_hosts, count * sizeof(struct hostd *));
  716.     wp->wa_spec = (void *)wxp;
  717.  
  718.     for (i = 0; i < count; i++) {
  719.         hp = hd_new(0);
  720.         wxp->w_hosts[i] = hp;
  721.         if (upkstralloc(mp, &buf)) {
  722.             pvmlogerror("addhosts() bad msg format\n");
  723.             goto bad;
  724.         }
  725.         if (parsehost(buf, hp)) {
  726.             hp->hd_err = PvmBadParam;
  727.  
  728.         } else {
  729.  
  730.         /* Set unspecified fields from hostfile if available */
  731.  
  732.             if (filehosts &&
  733.                     ((hp2 = nametohost(filehosts, hp->hd_name))
  734.                     || (hp2 = filehosts->ht_hosts[0])))
  735.                 applydefaults(hp, hp2);
  736.         }
  737.         PVM_FREE(buf);
  738.     }
  739.  
  740.     /*
  741.     * lookup IP addresses  XXX we already have some of them
  742.     */
  743.  
  744.     ngood = 0;
  745.     for (i = 0; i < count; i++) {
  746.         hp = wxp->w_hosts[i];
  747.         if (hp->hd_err)
  748.             continue;
  749.  
  750.         if (he = gethostbyname(hp->hd_aname ? hp->hd_aname : hp->hd_name)) {
  751.             BCOPY(he->h_addr_list[0], (char*)&hp->hd_sad.sin_addr,
  752.                     sizeof(struct in_addr));
  753.  
  754.         } else {
  755.             if (pvmdebmask & PDMSTARTUP) {
  756.                 pvmlogprintf(
  757.                         "start_slaves() can't gethostbyname: %s\n",
  758.                         hp->hd_name);
  759.             }
  760.             hp->hd_err = PvmNoHost;
  761.             continue;
  762.         }
  763.  
  764.     /* make sure it's not already configured */
  765.  
  766.         if (!(hp->hd_flag & HF_OVERLOAD)) {
  767.             for (hh = hosts->ht_last; hh > 0; hh--)
  768.                 if ((hp2 = hosts->ht_hosts[hh])
  769.                 && (hp2->hd_sad.sin_addr.s_addr == hp->hd_sad.sin_addr.s_addr)) {
  770.                     hp->hd_err = PvmDupHost;
  771.                     break;
  772.                 }
  773.             if (hp->hd_err)
  774.                 continue;
  775.  
  776.     /* make sure new ones aren't duplicated */
  777.  
  778.             for (j = i; j-- > 0; )
  779.                 if (hp->hd_sad.sin_addr.s_addr
  780.                 == wxp->w_hosts[j]->hd_sad.sin_addr.s_addr) {
  781.                     hp->hd_err = PvmDupHost;
  782.                     break;
  783.                 }
  784.             if (hp->hd_err)
  785.                 continue;
  786.         }
  787.  
  788.         ngood++;
  789.     }
  790.  
  791.     /*
  792.     * assign tids  XXX these are unreserved until hosts are added...
  793.     */
  794.  
  795.     ntid = ngood;
  796.     tids = TALLOC(ngood, int, "xxx");
  797.     hostids_new(&ntid, tids);
  798.     if (ntid < ngood) {
  799.         pvmlogerror("addhosts() out of hostids\n");
  800.         ngood = ntid;
  801.     }
  802.     for (j = i = 0; i < count; i++) {
  803.         hp = wxp->w_hosts[i];
  804.         if (hp->hd_err)
  805.             continue;
  806.         if (j < ntid)
  807.             hp->hd_hostpart = tids[j++];
  808.         else
  809.             hp->hd_err = PvmOutOfRes;
  810.     }
  811.     PVM_FREE(tids);
  812.  
  813. /*
  814.     if (!ngood) {
  815.         XXX don't really need to send the message
  816.     }
  817. */
  818.  
  819.     /* keep stub reply message to caller */
  820.  
  821.     wp->wa_mesg = rmp;
  822.  
  823.     /* make request message and send to hoster or pvmd' */
  824.  
  825.     mp2 = mesg_new(0);
  826.     mp2->m_wid = wp->wa_wid;
  827.     pkint(mp2, ngood);
  828.     if (!(pvmdpath = getenv("PVM_DPATH")))
  829.         pvmdpath = PVMDPATH;
  830.     for (i = 0; i < count; i++) {
  831.         hp = wxp->w_hosts[i];
  832.         if (hp->hd_err)
  833.             continue;
  834.         pkint(mp2, hp->hd_hostpart);
  835.         pkstr(mp2, hp->hd_sopts ? hp->hd_sopts : "");
  836.         if (hp->hd_login) {
  837.             len = strlen(hp->hd_login)
  838.                 + strlen((hp->hd_aname ? hp->hd_aname : hp->hd_name))
  839.                 + 2;
  840.             buf = TALLOC( len, char, "hdl" );
  841.             sprintf(buf, "%s@%s", hp->hd_login,
  842.                     (hp->hd_aname ? hp->hd_aname : hp->hd_name));
  843.         }
  844.         else
  845.             buf = STRALLOC( (hp->hd_aname
  846.                     ? hp->hd_aname : hp->hd_name) );
  847.         pkstr(mp2, buf);
  848.         PVM_FREE(buf);
  849.         len = strlen( (hp->hd_dpath ? hp->hd_dpath : pvmdpath) ) + 1
  850.             + strlen( (hp->hd_sopts && !strcmp(hp->hd_sopts, "ms")
  851.                     ? "-S" : "-s") ) + 1
  852.             + 2 + 16 + 1
  853.             + 2 + strlen( hp->hd_name ) + 1
  854.             + 5 * ( 16 + 1 );
  855.         buf = TALLOC( len, char, "hdall" );
  856.         (void)sprintf(buf, "%s %s -d%x -n%s %d %s %d",
  857.                 (hp->hd_dpath ? hp->hd_dpath : pvmdpath),
  858.                 (hp->hd_sopts && !strcmp(hp->hd_sopts, "ms")
  859.                     ? "-S" : "-s"),
  860.                 pvmdebmask,
  861.                 hp->hd_name,
  862.                 hosts->ht_master,
  863.                 inadport_hex(&hosts->ht_hosts[hosts->ht_master]->hd_sad),
  864.                 hosts->ht_hosts[hosts->ht_master]->hd_mtu);
  865.         (void)sprintf(buf + strlen(buf), " %d %s",
  866.                 ((hp->hd_hostpart & tidhmask) >> (ffs(tidhmask) - 1)),
  867.                 inadport_hex(&hp->hd_sad));
  868.         pkstr(mp2, buf);
  869.         PVM_FREE(buf);
  870.     }
  871.     mp2->m_tag = SM_STHOST;
  872.  
  873. #if !defined (WIN32) && !defined (IMA_OS2)
  874.  
  875.     if (hostertid) {
  876.         hosterwid = wp->wa_wid;
  877.         mp2->m_dst = hostertid;
  878.         wp->wa_on = hostertid;
  879.         sendmessage(mp2);
  880.  
  881.     } else {
  882.         wp->wa_on = TIDPVMD;
  883.  
  884.         if (pid = fork()) {        /* still us */
  885.             if (pid == -1) {
  886.     /* nack request if can't fork */
  887.                 pvmlogperror("addhosts() fork");
  888.                 goto bad;
  889.  
  890.             } else {
  891.                 pprime = pid;
  892.                 pmsg_unref(mp2);
  893.             }
  894.  
  895.         } else {                /* pvmd' to do the startup */
  896.             beprime();
  897.             mesg_rewind(mp2);
  898.             hoster(mp2);
  899.         }
  900.     }
  901.  
  902. #else /* WIN32 */
  903.  
  904.     if (!hostertid)    {            /* no hoster yet */
  905.         hostertid = tid_new();    /* give him a tid so that we can send
  906.                                     him the message */
  907.  
  908.         if (start_hoster(hostertid) == -1) {
  909.             hostertid=0;            /* you did not make it buddy */
  910.             pvmlogperror("addhosts(): could not start hoster \n");
  911.             goto bad;
  912.         }
  913.     }
  914.     hosterwid = wp->wa_wid;
  915.     mp2->m_dst = hostertid;
  916.     wp->wa_on = hostertid;
  917.     sendmessage(mp2);
  918.  
  919. #endif
  920.  
  921.     return 0;
  922.  
  923. bad:
  924.     if (wxp)
  925.         free_waitc_add(wxp);
  926.     if (wp) {
  927.         wp->wa_mesg = 0;    /* shared with rmp */
  928.         wait_delete(wp);
  929.     }
  930.     busyadding = 0;
  931.     pkint(rmp, PvmDSysErr);
  932.     sendmessage(rmp);
  933.     return 0;
  934. }
  935.  
  936. #ifdef WIN32
  937.  
  938. /*    start_hoster()
  939. *
  940. *    forkexec the hoster process which
  941. *    will start other pvmd's.
  942. *    acts like pvmd'
  943. */
  944.  
  945. int
  946. start_hoster(reserved_tid)
  947.     int reserved_tid;
  948. {
  949.     char hosterpath[128];
  950.  
  951.     (void) strcpy(hosterpath,(char *) pvmgetroot());
  952.     (void) strcat(hosterpath,"/bin/WIN32/hoster.exe");
  953.  
  954.     if (hostexectasker(hosterpath,reserved_tid))
  955.         return -1;
  956.  
  957.     return 0;
  958. }
  959.  
  960. int
  961. hostexectasker(file, tid)
  962.     char *file;
  963.     int tid;
  964. {
  965.     int tids = 0;            /* tid from hosterforkexec */
  966.     
  967.     struct pmsg *rmp;
  968.     struct task *tp = 0;
  969.     int err = 0;            /* error code from forkexec */
  970.  
  971.     rmp = mesg_new(0);
  972.     rmp->m_dst = pvmmytid;
  973.     rmp->m_src = pvmmytid;
  974.  
  975.     rmp->m_tag = DM_EXECACK;
  976.  
  977.     if (err = hosterforkexec(tid,file, &tp)) {
  978.         tids = err;
  979.     } else {
  980.             tp->t_ptid = 0;
  981.             tp->t_outtid = 0;
  982.             tp->t_trctid = 0;
  983.             tp->t_sched = 0;
  984.             tids = tp->t_tid;
  985.     }
  986.  
  987.     pkint(rmp, 1);
  988.     pkint(rmp, tids);
  989.     sendmessage(rmp);
  990.     
  991.     return err;
  992. }
  993.  
  994. extern char **environ;
  995.  
  996. int
  997. hosterforkexec(tid, name, tpp)
  998.     int tid;                /* tid given by hosterexectask */
  999.     char *name;                /* filename */
  1000.     struct task **tpp;        /* return task context */
  1001. {
  1002.     int pid = -1;                /* task pid */
  1003.     char *argv[2];
  1004.     struct task *tp;        /* new task context */
  1005.     char *expected_pid;
  1006.     char buf[32];
  1007.     char *myenv[100];
  1008.     HANDLE hpid;
  1009.     char **p, **q;
  1010.     struct stat sb;    
  1011.     extern int *ptr_nfp;        /* XXX fix this */
  1012.     SECURITY_ATTRIBUTES saPipe;
  1013.     PROCESS_INFORMATION pi;
  1014.     STARTUPINFO si;  /* for CreateProcess call */
  1015.  
  1016.     if (stat(name, &sb) == -1) 
  1017.         return PvmNoFile;
  1018.  
  1019.     tp = task_new(tid);        
  1020.  
  1021.     p = myenv;
  1022.     q = environ;
  1023.     while (*q) {
  1024.         *p++ = *q++;       
  1025.     }
  1026.  
  1027.     /* copy all the environment for socket stuff and more */
  1028.  
  1029.     expected_pid=malloc(20 * sizeof(char));
  1030.     sprintf(expected_pid, "PVMEPID=%d", *ptr_nfp);
  1031.         
  1032.     *p++ = expected_pid;
  1033.     *p=0;
  1034.     pvmputenv(expected_pid);
  1035.     argv[0]=name;
  1036.     argv[1]=0;
  1037.     saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
  1038.     saPipe.lpSecurityDescriptor = NULL;
  1039.     saPipe.bInheritHandle = FALSE;
  1040.  
  1041.     memset(&si, 0, sizeof(si));
  1042.     si.cb = sizeof(si);
  1043.     if (0) {
  1044.         pid = CreateProcess(name,  /* filename */
  1045.             NULL,  /* full command line for child */
  1046.             NULL,  /* process security descriptor */
  1047.             NULL,  /* thread security descriptor */
  1048.             FALSE,  /* inherit handles? */
  1049.             DETACHED_PROCESS,  /* creation flags */
  1050.             NULL,  /* inherited environment address */
  1051.             NULL,  /* startup dir; NULL = start in current */
  1052.             &si,  /* pointer to startup info (input) */
  1053.             &pi);  /* pointer to process info (output) */
  1054.             
  1055.             /* fprintf(stderr,"yup \n"); */
  1056.     }
  1057.     else
  1058.     pid = _spawnve(_P_NOWAIT,name,argv,myenv); 
  1059.                                   
  1060.     if (pid == -1) {
  1061.             pvmlogperror("forkexec_hoster() _spawnve");
  1062.             /* task_free(&tp); */
  1063.             pvmbailout(0);
  1064.             return PvmOutOfRes;
  1065.     }
  1066.         
  1067.     task_setpid(tp,*ptr_nfp);
  1068.     *ptr_nfp=*ptr_nfp + 1;
  1069.  
  1070.     tp->t_flag |= TF_FORKD;
  1071.  
  1072.     tp->t_a_out = STRALLOC(name);
  1073.      
  1074.     *tpp = tp;
  1075.     
  1076.     return 0;
  1077. }        
  1078.  
  1079. #endif 
  1080.  
  1081. /*    dm_add()
  1082. *
  1083. *    (Master pvmd) gets request to add new hosts.
  1084. *DocThis
  1085. *    DM_ADD(wid) {
  1086. *        int count
  1087. *        string name[count]
  1088. *    }
  1089. *EndDocThis
  1090. */
  1091.  
  1092. int
  1093. dm_add(hp, mp)
  1094.     struct hostd *hp;
  1095.     struct pmsg *mp;
  1096. {
  1097.     struct pmsg *rmp;
  1098.  
  1099.     rmp = mesg_new(0);
  1100.     rmp->m_dst = mp->m_src;
  1101.     rmp->m_ctx = mp->m_ctx;
  1102.     rmp->m_tag = DM_ADDACK;
  1103.     rmp->m_wid = mp->m_wid;
  1104.     addhosts(mp, rmp);
  1105.     return 0;
  1106. }
  1107.  
  1108.  
  1109. /*    dm_addack()
  1110. *
  1111. *    Reply to DM_ADD operation.
  1112. *
  1113. *    DM_ADDACK(wid_rtn) {
  1114. *        int nhosts            // or error code
  1115. *        int narches            // if nhosts >= 0
  1116. *        {
  1117. *            int tid            // or error code
  1118. *            string name
  1119. *            string arch
  1120. *            int mtu
  1121. *            int speed
  1122. *        } [nhosts]
  1123. *    }
  1124. */
  1125.  
  1126. int
  1127. dm_addack(hp, mp)
  1128.     struct hostd *hp;
  1129.     struct pmsg *mp;
  1130. {
  1131.     struct waitc *wp;
  1132.  
  1133.     if (!(wp = wait_get(hp, mp, WT_ADDHOST)))
  1134.         return 0;
  1135.     pmsg_packbody(wp->wa_mesg, mp);
  1136.     sendmessage(wp->wa_mesg);
  1137.     wp->wa_mesg = 0;
  1138.     wait_delete(wp);
  1139.     return 0;
  1140. }
  1141.  
  1142.  
  1143. /*    exectasks()
  1144. *
  1145. *    Start tasks requested with DM_EXEC or SM_EXEC.
  1146. *    Call the tasker or start 'em.
  1147. *    We try to fork/exec given number of tasks.  On first error, fill
  1148. *    remainder of tid array with error code.
  1149. *    Return tid array to caller.
  1150. */
  1151.  
  1152. int
  1153. exectasks(mp, rmp, schtid)
  1154.     struct pmsg *mp;        /* the request message */
  1155.     struct pmsg *rmp;        /* reply message blank */
  1156.     int schtid;                /* scheduler for new tasks */
  1157. {
  1158.     struct pmsg *mp2;            /* reply message hdl */
  1159.     int i;
  1160.     struct timeval now;
  1161.     struct waitc_spawn *wxp;    /* new task parameters */
  1162.     int munge_tenv = 0;
  1163.     char tmp[255];
  1164.  
  1165.     wxp = TALLOC(1, struct waitc_spawn, "waix");
  1166.     BZERO((char*)wxp, sizeof(struct waitc_spawn));
  1167.  
  1168.     /* unpack message */
  1169.  
  1170.     if (upkuint(mp, &wxp->w_ptid)
  1171.     || upkstralloc(mp, &wxp->w_file)
  1172.     || upkint(mp, &wxp->w_flags)
  1173.     || upkint(mp, &wxp->w_veclen)
  1174.     || upkint(mp, &wxp->w_argc))
  1175.         goto bad;
  1176.  
  1177.     if (wxp->w_veclen < 1)
  1178.         goto bad;
  1179.     wxp->w_vec = TALLOC(wxp->w_veclen, int, "tids");
  1180.  
  1181.     wxp->w_argc += 2;
  1182.     wxp->w_argv = TALLOC(wxp->w_argc + 1, char*, "argv");
  1183.     BZERO((char*)wxp->w_argv, (wxp->w_argc + 1) * sizeof(char*));
  1184.     wxp->w_argv++;
  1185.     wxp->w_argv[0] = wxp->w_file;
  1186.     wxp->w_file = 0;
  1187.     wxp->w_argv[--wxp->w_argc] = 0;
  1188.     for (i = 1; i < wxp->w_argc; i++)
  1189.         if (upkstralloc(mp, &wxp->w_argv[i]))
  1190.             goto bad;
  1191.  
  1192.     if (upkuint(mp, &wxp->w_outtid)
  1193.     || upkuint(mp, &wxp->w_outctx)
  1194.     || upkuint(mp, &wxp->w_outtag)
  1195.     || upkuint(mp, &wxp->w_trctid)
  1196.     || upkuint(mp, &wxp->w_trcctx)
  1197.     || upkuint(mp, &wxp->w_trctag)
  1198.     || upkuint(mp, &wxp->w_nenv))
  1199.         goto bad;
  1200.  
  1201.     wxp->w_hosttotal = wxp->w_veclen; /* this is the task count! */
  1202.  
  1203.     if (pvmtracer.trctid) {
  1204.         if (!(wxp->w_trctid) && pvmtracer.trctag) {
  1205.             wxp->w_trctid = pvmtracer.trctid;
  1206.             wxp->w_trcctx = pvmtracer.trcctx;
  1207.             wxp->w_trctag = pvmtracer.trctag;
  1208.             munge_tenv++;
  1209.         }
  1210.     }
  1211.  
  1212.     if (pvmtracer.outtid) {
  1213.         if (!(wxp->w_outtid) && pvmtracer.outtag) {
  1214.             wxp->w_outtid = pvmtracer.outtid;
  1215.             wxp->w_outctx = pvmtracer.outctx;
  1216.             wxp->w_outtag = pvmtracer.outtag;
  1217.         }
  1218.     }
  1219.  
  1220.     wxp->w_env = TALLOC((wxp->w_nenv + 1), char*, "env");
  1221.     BZERO(wxp->w_env, (wxp->w_nenv + 1) * sizeof(char*));
  1222.  
  1223.     for (i = 0; i < wxp->w_nenv; i++)
  1224.         if (upkstralloc(mp, &wxp->w_env[i]))
  1225.             goto bad;
  1226.     if ( upkuint(mp, &wxp->w_instance) || upkuint(mp, &wxp->w_outof))
  1227.         goto bad;
  1228.  
  1229.     if ( munge_tenv ) {
  1230.         sprintf( tmp, "PVMTMASK=%s", pvmtracer.tmask );
  1231.         pvmenvinsert( &(wxp->w_env), tmp );
  1232.  
  1233.         sprintf( tmp, "PVMTRCBUF=%d", pvmtracer.trcbuf );
  1234.         pvmenvinsert( &(wxp->w_env), tmp );
  1235.  
  1236.         sprintf( tmp, "PVMTRCOPT=%d", pvmtracer.trcopt );
  1237.         pvmenvinsert( &(wxp->w_env), tmp );
  1238.  
  1239.         for ( wxp->w_nenv = 0 ; wxp->w_env[ wxp->w_nenv ] ;
  1240.             (wxp->w_nenv)++ );
  1241.     }
  1242.  
  1243.     /* call ppi_load to get tasks running */
  1244.  
  1245.     wxp->w_sched = schtid;
  1246.  
  1247. #if defined(IMA_PGON) || defined(IMA_SP2MPI)
  1248.     if (!(wxp->w_flags & PvmMppFront))
  1249.     {
  1250.         mpp_load(wxp);
  1251.     }
  1252.     else
  1253. #endif
  1254.     {
  1255.         ppi_load(wxp);
  1256.     }
  1257.  
  1258. for (i = 0; i < wxp->w_veclen; i++) {
  1259.         if (wxp->w_vec[i] > 0) {
  1260.             if (wxp->w_trctid > 0) {
  1261.                 tev_send_newtask(
  1262.                     wxp->w_trctid, wxp->w_trcctx, wxp->w_trctag,
  1263.                     wxp->w_vec[i], wxp->w_ptid, wxp->w_flags,
  1264.                     wxp->w_argv[0] );
  1265.             }
  1266.             if (wxp->w_outtid > 0) {
  1267.                 mp2 = mesg_new(0);
  1268.                 mp2->m_dst = wxp->w_outtid;
  1269.                 mp2->m_ctx = wxp->w_outctx;
  1270.                 mp2->m_tag = wxp->w_outtag;
  1271.                 pkint(mp2, wxp->w_vec[i]);
  1272.                 pkint(mp2, -2);
  1273.                 pkint(mp2, wxp->w_ptid);
  1274.                 sendmessage(mp2);
  1275.             }
  1276.         }
  1277.     }
  1278.  
  1279.     pkint(rmp, wxp->w_veclen);
  1280.     for (i = 0; i < wxp->w_veclen; i++)
  1281.         pkint(rmp, wxp->w_vec[i]);
  1282.     sendmessage(rmp);
  1283.     goto cleanup;
  1284.  
  1285. bad:
  1286.     pvmlogprintf("exectasks() from t%x bad msg format\n", mp->m_src);
  1287.  
  1288. cleanup:
  1289.     if (wxp->w_argv)
  1290.         *--wxp->w_argv = 0;
  1291.     free_wait_spawn(wxp);
  1292.     return 0;
  1293. }
  1294.  
  1295.  
  1296. /*    dm_exec()
  1297. *
  1298. *    Request to start task processes.
  1299. *
  1300. *    DM_EXEC(wid) {
  1301. *        int ptid
  1302. *        string file
  1303. *        int flags
  1304. *        int count
  1305. *        int nargs
  1306. *        string argv[nargs]
  1307. *        int outtid
  1308. *        int outctx
  1309. *        int outtag
  1310. *        int trctid
  1311. *        int trcctx
  1312. *        int trctag
  1313. *        int nenv
  1314. *        string env[nenv]
  1315. *    }
  1316. */
  1317.  
  1318. int
  1319. dm_exec(hp, mp)
  1320.     struct hostd *hp;
  1321.     struct pmsg *mp;
  1322. {
  1323.     struct pmsg *rmp;
  1324.  
  1325.     hp = hp;
  1326.  
  1327.     rmp = mesg_new(0);
  1328.     rmp->m_dst = mp->m_src;
  1329.     rmp->m_ctx = mp->m_ctx;
  1330.     rmp->m_tag = DM_EXECACK;
  1331.     rmp->m_wid = mp->m_wid;
  1332.     exectasks(mp, rmp, pvmschedtid);
  1333.     return 0;
  1334. }
  1335.  
  1336.  
  1337. /*    dm_execack()
  1338. *
  1339. *    Reply to DM_EXEC op.
  1340. *
  1341. *    DM_EXECACK(wid_rtn) {
  1342. *        int count
  1343. *        int tids[count]
  1344. *    }
  1345. */
  1346.  
  1347. int
  1348. dm_execack(hp, mp)
  1349.     struct hostd *hp;
  1350.     struct pmsg *mp;
  1351. {
  1352.     struct waitc *wp;
  1353.     struct waitc_spawn *wxp;
  1354.     int rcnt;                /* num of tids+errors returned */
  1355.     int tid;
  1356.     int v;
  1357.     int err = 0;
  1358.     int i;
  1359.  
  1360.     if (!(wp = wait_get(hp, mp, WT_SPAWN)))
  1361.         return 0;
  1362.  
  1363.     wxp = (struct waitc_spawn*)wp->wa_spec;
  1364.     if (upkint(mp, &rcnt))
  1365.         goto bad;
  1366.     v = wxp->w_veclen;
  1367.  
  1368.     /*
  1369.     * unpack tids and place in result vector where hostpart is now
  1370.     */
  1371.  
  1372. /*
  1373.     pvmlogprintf("dm_execack() hp %x vec len %d, repl len %d\n",
  1374.             hp->hd_hostpart, v, rcnt);
  1375. */
  1376.     i = 0;
  1377.     while (rcnt-- > 0) {
  1378.         if (upkint(mp, &tid))
  1379.             goto bad;
  1380.         if (tid < 0)
  1381.             err++;
  1382.         while (i < v && wxp->w_vec[i] != hp->hd_hostpart)
  1383.             i++;
  1384.         if (i == v) {
  1385.             pvmlogerror("dm_execack() tids don't fit result vector?\n");
  1386.             wait_delete(wp);
  1387.             return 0;
  1388.         }
  1389.         wxp->w_vec[i++] = tid;
  1390.     }
  1391.  
  1392.     if (err)
  1393.         ht_delete(wxp->w_ht, hp);
  1394.  
  1395.     /*
  1396.     * if everyone has checked in, either restart the failed ones
  1397.     * or reply to the caller.
  1398.     */
  1399.  
  1400.     if (wp->wa_peer == wp)
  1401.         assign_tasks(wp);
  1402.     wait_delete(wp);
  1403.     return 0;
  1404.  
  1405. bad:
  1406.     pvmlogprintf("dm_execack() from 0x%x bad msg format\n", mp->m_src);
  1407.     wait_delete(wp);
  1408.     return 0;
  1409. }
  1410.  
  1411.  
  1412. /*    dm_sendsig()
  1413. *
  1414. *    Request to send signal to local task.
  1415. *
  1416. *    DM_SENDSIG {
  1417. *        int tid
  1418. *        int signum
  1419. *    }
  1420. */
  1421.  
  1422. int
  1423. dm_sendsig(hp, mp)
  1424.     struct hostd *hp;
  1425.     struct pmsg *mp;
  1426. {
  1427.     int tid;
  1428.     int signum;
  1429.     struct task *tp;
  1430.  
  1431.     hp = hp;
  1432.  
  1433.     if (upkuint(mp, &tid) || upkint(mp, &signum)) {
  1434.         pvmlogerror("dm_sendsig() bad msg format\n");
  1435.         return 0;
  1436.     }
  1437.     if (tp = task_find(tid)) {
  1438.         ppi_kill(tp, signum);
  1439.  
  1440.     } else
  1441.         if (pvmdebmask & (PDMTASK|PDMAPPL)) {
  1442.             pvmlogprintf("dm_sendsig() signal for t%x scrapped\n", tid);
  1443.         }
  1444.     return 0;
  1445. }
  1446.  
  1447.  
  1448. /*    dm_htupd()
  1449. *
  1450. *    Host table update phase 1 - (Non-master) pvmd is notified of new
  1451. *    host table.
  1452. *
  1453. *    DM_HTUPD(wid) {
  1454. *        int serial
  1455. *        int master
  1456. *        int console
  1457. *        int count
  1458. *        {
  1459. *            int index
  1460. *            string name
  1461. *            string arch
  1462. *            string hexipaddr
  1463. *            int mtu
  1464. *            int speed
  1465. *            int dsig
  1466. *        } [count]
  1467. *    }
  1468. */
  1469.  
  1470. int
  1471. dm_htupd(hp, mp)
  1472.     struct hostd *hp;
  1473.     struct pmsg *mp;
  1474. {
  1475.     int count;            /* number of hosts in message */
  1476.     int hh;
  1477.     char buf[16];        /* for converting sockaddr */
  1478.     struct pmsg *mp2;
  1479.  
  1480.     /* unpack new host table params */
  1481.  
  1482.     newhosts = ht_new(1);
  1483.     newhosts->ht_local = hosts->ht_local;
  1484.     upkint(mp, &newhosts->ht_serial);
  1485.     upkint(mp, &newhosts->ht_master);
  1486.     upkint(mp, &newhosts->ht_cons);
  1487.  
  1488.     /* add current hosts to the table */
  1489.  
  1490.     ht_merge(newhosts, hosts);
  1491.  
  1492.     /* unpack new hosts and add to table */
  1493.  
  1494.     /* XXX if we find a host already in table we should kill it with
  1495.        XXX hostfail and put the new one in its place */
  1496.  
  1497.     upkint(mp, &count);
  1498.     while (count-- > 0) {
  1499.         upkint(mp, &hh);
  1500.         hp = hd_new(hh);
  1501.         upkstralloc(mp, &hp->hd_name);
  1502.         upkstralloc(mp, &hp->hd_arch);
  1503.         upkstr(mp, buf, sizeof(buf));
  1504.         hex_inadport(buf, &hp->hd_sad);
  1505.         upkint(mp, &hp->hd_mtu);
  1506.         upkint(mp, &hp->hd_speed);
  1507.         upkint(mp, &hp->hd_dsig);
  1508.         ht_insert(newhosts, hp);
  1509.         hd_unref(hp);
  1510.     }
  1511.  
  1512.     if (pvmdebmask & PDMHOST) {
  1513.         pvmlogerror("dm_htupd() new host table:\n");
  1514.         ht_dump(newhosts);
  1515.     }
  1516.     runstate = PVMDHTUPD;
  1517.  
  1518.     /* reply to sender that we have new host table */
  1519.  
  1520.     mp2 = mesg_new(0);
  1521.     mp2->m_dst = mp->m_src;
  1522.     mp2->m_ctx = mp->m_ctx;
  1523.     mp2->m_tag = DM_HTUPDACK;
  1524.     mp2->m_wid = mp->m_wid;
  1525.     sendmessage(mp2);
  1526.     return 0;
  1527. }
  1528.  
  1529.  
  1530. /*    dm_htupdack()
  1531. *
  1532. *    Reply to DM_HTUPD op.
  1533. *
  1534. *    DM_HTUPDACK(wid_rtn) { }
  1535. */
  1536.  
  1537. int
  1538. dm_htupdack(hp, mp)
  1539.     struct hostd *hp;
  1540.     struct pmsg *mp;
  1541. {
  1542.     struct waitc *wp;
  1543.  
  1544.     if (!(wp = wait_get(hp, mp, WT_HTUPD)))
  1545.         return 0;
  1546.  
  1547.     /* is this is the last host checking in, send ht commit */
  1548.  
  1549.     if (wp->wa_peer == wp) {
  1550.         int hh;
  1551.  
  1552.         mp = mesg_new(0);
  1553.         mp->m_tag = DM_HTCOMMIT;
  1554.  
  1555.         for (hh = hosts->ht_last; hh > 0; hh--)
  1556.             if (hh != hosts->ht_local && (hp = hosts->ht_hosts[hh])) {
  1557.                 mp->m_ref++;
  1558.                 mp->m_dst = hp->hd_hostpart | TIDPVMD;
  1559.                 sendmessage(mp);
  1560.             }
  1561.         pmsg_unref(mp);
  1562.  
  1563.         busyadding = 0;
  1564.         sendmessage(wp->wa_mesg);
  1565.         wp->wa_mesg = 0;
  1566.     }
  1567.     wait_delete(wp);
  1568.     return 0;
  1569. }
  1570.  
  1571.  
  1572. /*    ht_diff()
  1573. *
  1574. *    Generates a host table containing entries in ht2 but not in ht1.
  1575. */
  1576.  
  1577. struct htab *
  1578. ht_diff(htp2, htp1)
  1579.     struct htab *htp2;        /* more */
  1580.     struct htab *htp1;        /* less */
  1581. {
  1582.     struct htab *htp;
  1583.     int hh;
  1584.  
  1585.     htp = ht_new(1);
  1586.     for (hh = htp2->ht_last; hh > 0; hh--)
  1587.         if (htp2->ht_hosts[hh] && !htp2->ht_hosts[hh]->hd_err
  1588.         && (hh > htp1->ht_last || !htp1->ht_hosts[hh]))
  1589.             ht_insert(htp, htp2->ht_hosts[hh]);
  1590.     return htp;
  1591. }
  1592.  
  1593.  
  1594. /*    gotnewhosts()
  1595. *
  1596. *    Used to wake up any waitcs of kind WT_HOSTA.
  1597. *    Sends a message containing count and list of new d-tids.
  1598. */
  1599.  
  1600. gotnewhosts(htp2, htp1)
  1601.     struct htab *htp2;        /* new host table */
  1602.     struct htab *htp1;        /* old host table */
  1603. {
  1604.     struct pmsg *mp;
  1605.     struct htab *htp;
  1606.     struct waitc *wp, *wp2;
  1607.     int hh;
  1608.  
  1609.     mp = 0;
  1610.     for (wp = waitlist->wa_link; wp != waitlist; wp = wp2) {
  1611.         wp2 = wp->wa_link;
  1612.         if (wp->wa_kind == WT_HOSTA) {
  1613.             if (!mp) {
  1614.                 mp = mesg_new(0);
  1615.                 htp = ht_diff(htp2, htp1);
  1616.                 pkint(mp, htp->ht_cnt);
  1617.                 for (hh = htp->ht_last; hh > 0; hh--)
  1618.                     if (htp->ht_hosts[hh])
  1619.                         pkint(mp, htp->ht_hosts[hh]->hd_hostpart);
  1620.                 ht_free(htp);
  1621.             }
  1622.             mp->m_ref++;
  1623.             mp->m_dst = wp->wa_mesg->m_dst;
  1624.             mp->m_ctx = wp->wa_mesg->m_ctx;
  1625.             mp->m_tag = wp->wa_mesg->m_tag;
  1626.             sendmessage(mp);
  1627.             if (wp->wa_count != -1 && --wp->wa_count < 1)
  1628.                 wait_delete(wp);
  1629.         }
  1630.     }
  1631.     if (mp)
  1632.         pmsg_unref(mp);
  1633.     return 0;
  1634. }
  1635.  
  1636.  
  1637. /*    dm_htcommit()
  1638. *
  1639. *    Host table update phase 2 - commit to new host table.
  1640. *
  1641. *    DM_HTCOMMIT { }
  1642. */
  1643.  
  1644. int
  1645. dm_htcommit(hp, mp)
  1646.     struct hostd *hp;
  1647.     struct pmsg *mp;
  1648. {
  1649.     struct htab *htp;
  1650.  
  1651.     if (hp != hosts->ht_hosts[hosts->ht_master]) {
  1652.         pvmlogprintf("dm_htcommit() from t%x (not master)?\n",
  1653.                 mp->m_src);
  1654.     }
  1655.  
  1656.     if (newhosts) {
  1657.         htp = hosts;
  1658.         hosts = newhosts;
  1659.         newhosts = 0;
  1660.         if (pvmdebmask & PDMHOST) {
  1661.             pvmlogprintf(
  1662.             "dm_htcommit() committing from host table serial %d to %d\n",
  1663.                     htp->ht_serial, hosts->ht_serial);
  1664.         }
  1665.  
  1666.         gotnewhosts(hosts, htp);
  1667.         ht_free(htp);
  1668.         runstate = PVMDNORMAL;
  1669.  
  1670.     } else {
  1671.         pvmlogerror("dm_htcommit() no new host table pending?\n");
  1672.     }
  1673.     return 0;
  1674. }
  1675.  
  1676.  
  1677. /*    dm_slconf()
  1678. *
  1679. *    Pvmd gets config info (from master).
  1680. *    One of these should arrive before a pvmd is fully up, even
  1681. *    before the host table update, to set private params.
  1682. *    More may be used later to set random knobs.
  1683. *
  1684. *    DM_SLCONF {
  1685. *        {
  1686. *            int fieldtype        // from ddpro.h
  1687. *            string value
  1688. *        } []                    // implied
  1689. *    }
  1690. */
  1691.  
  1692. int
  1693. dm_slconf(hp, mp)
  1694.     struct hostd *hp;
  1695.     struct pmsg *mp;
  1696. {
  1697.     int t;                /* field type */
  1698.     char *s, *s2;
  1699.  
  1700.     mp = mp;
  1701.     hp = hp;
  1702.  
  1703.     while (!upkint(mp, &t) && !upkstralloc(mp, &s)) {
  1704.         switch (t) {
  1705.  
  1706.         case DM_SLCONF_EP:
  1707.             if (pvmdebmask & (PDMTASK|PDMSTARTUP)) {
  1708.                 pvmlogprintf("dm_slconf() ep<%s>\n", s);
  1709.             }
  1710.             epaths = colonsep(varsub(s));
  1711.             PVM_FREE(s);
  1712.             break;
  1713.  
  1714.         case DM_SLCONF_BP:
  1715.             if (pvmdebmask & PDMSTARTUP) {
  1716.                 pvmlogprintf("dm_slconf() bp<%s>\n", s);
  1717.             }
  1718.             debugger = varsub(s);
  1719.             PVM_FREE(s);
  1720.             break;
  1721.  
  1722.         case DM_SLCONF_WD:
  1723.             if (pvmdebmask & (PDMTASK|PDMSTARTUP)) {
  1724.                 pvmlogprintf("dm_slconf() wd<%s>\n", s);
  1725.             }
  1726.             s2 = varsub(s);
  1727.             if (chdir(s2) == -1)
  1728.                 pvmlogperror(s2);
  1729.             PVM_FREE(s);
  1730.             PVM_FREE(s2);
  1731.             break;
  1732.  
  1733.         case DM_SLCONF_SCHED:
  1734.             if (pvmdebmask & (PDMSCHED|PDMSTARTUP)) {
  1735.                 pvmlogprintf("dm_slconf() sched<t%x>\n", pvmschedtid);
  1736.             }
  1737.             pvmschedtid = pvmxtoi(s);
  1738.             break;
  1739.  
  1740.         case DM_SLCONF_TRACE: {
  1741.             Pvmtmask tmask;
  1742.             int ttid, tctx, ttag, otid, octx, otag, tbuf, topt;
  1743.             if (pvmdebmask & (PDMTRACE|PDMSTARTUP)) {
  1744.                 pvmlogprintf("dm_slconf() tracer<t%x>\n",
  1745.                     pvmtracer.trctid);
  1746.             }
  1747.             if (sscanf(s, "%x %d %d %x %d %d %d %d %s",
  1748.                     &ttid, &tctx, &ttag, &otid, &octx, &otag,
  1749.                     &tbuf, &topt, tmask) != 9) {
  1750.                 pvmlogprintf("dm_slconf() bogus string<%s>\n", s);
  1751.             }
  1752.             else {
  1753.                 pvmtracer.trctid = ttid;
  1754.                 pvmtracer.trcctx = tctx;
  1755.                 pvmtracer.trctag = ttag;
  1756.                 pvmtracer.outtid = otid;
  1757.                 pvmtracer.outctx = octx;
  1758.                 pvmtracer.outtag = otag;
  1759.                 pvmtracer.trcbuf = tbuf;
  1760.                 pvmtracer.trcopt = topt;
  1761.                 BCOPY(tmask,pvmtracer.tmask,TEV_MASK_LENGTH);
  1762.             }
  1763.             break;
  1764.         }
  1765.  
  1766.         default:
  1767.             pvmlogprintf("dm_slconf() ? type %d val <%s>\n", t, s);
  1768.             PVM_FREE(s);
  1769.             break;
  1770.         }
  1771.     }
  1772.     return 0;
  1773. }
  1774.  
  1775.  
  1776. /*    startack()
  1777. *
  1778. *    Take results from pvmd' or hoster.  Update the machine config
  1779. *    if any were successful.  Reply to the DM_ADD or SM_ADD request.
  1780. */
  1781.  
  1782. int
  1783. startack(wp, mp)
  1784.     struct waitc *wp;        /* wait context on hoster */
  1785.     struct pmsg *mp;
  1786. {
  1787.     struct hostd *hp;
  1788.     struct pmsg *mp2;
  1789.     struct waitc *wp2;        /* seed waitc for htupd peer group */
  1790.     struct waitc *wp3;
  1791.     int count;                /* num of new hosts */
  1792.     int happy;                /* num of happy new hosts */
  1793.     struct waitc_add *wxp;
  1794.     char *av[16];            /* for reply parsing */
  1795.     int ac;
  1796.     int ver;
  1797.     int i, j;
  1798.     int t;
  1799.     int hh;
  1800.     char *buf;
  1801.     char buf2[256];
  1802.  
  1803.     /*
  1804.     * unpack startup results, update hosts in wait context
  1805.     */
  1806.  
  1807.     wxp = (struct waitc_add *)wp->wa_spec;
  1808.     count = wxp->w_num;
  1809.     upkint(mp, &j);
  1810.  
  1811.     while (j-- > 0) {
  1812.         if (upkuint(mp, &t) || upkstralloc(mp, &buf)) {
  1813.             pvmlogerror("startack() bad message format\n");
  1814.             pkint(wp->wa_mesg, PvmDSysErr);
  1815.             sendmessage(wp->wa_mesg);
  1816.             wp->wa_mesg = 0;
  1817.             wait_delete(wp);
  1818.             busyadding = 0;
  1819.             return 0;
  1820.         }
  1821.         for (i = count; i-- > 0 && wxp->w_hosts[i]->hd_hostpart != t; ) ;
  1822.         if (i < 0) {
  1823.             pvmlogprintf("startack() what? some random tid %x\n", t);
  1824.             pkint(wp->wa_mesg, PvmDSysErr);
  1825.             sendmessage(wp->wa_mesg);
  1826.             wp->wa_mesg = 0;
  1827.             wait_delete(wp);
  1828.             busyadding = 0;
  1829.             PVM_FREE(buf);
  1830.             return 0;
  1831.         }
  1832.         hp = wxp->w_hosts[i];
  1833.         ac = sizeof(av)/sizeof(av[0]);
  1834.         if (crunchzap(buf, &ac, av) || ac != 5) {
  1835.             pvmlogprintf("startack() host %s expected version, got \"%s\"\n",
  1836.                     hp->hd_name, buf);
  1837.             if (!(hp->hd_err = errnamecode(buf)))
  1838.                 hp->hd_err = PvmCantStart;
  1839.             PVM_FREE(buf);
  1840.             continue;
  1841.         }
  1842.  
  1843.         ver = atoi(av[0]);
  1844.         if (ver != DDPROTOCOL) {
  1845.             pvmlogprintf(
  1846.                     "slave_exec() host %s d-d protocol mismatch (%d/%d)\n",
  1847.                     hp->hd_name, ver, DDPROTOCOL);
  1848.             hp->hd_err = PvmBadVersion;
  1849.             continue;
  1850.         }
  1851.  
  1852.         hp->hd_arch = STRALLOC(av[1]);
  1853.         hex_inadport(av[2], &hp->hd_sad);
  1854.         hp->hd_mtu = atoi(av[3]);
  1855.         hp->hd_dsig = atoi(av[4]);
  1856.  
  1857.         PVM_FREE(buf);
  1858.     }
  1859.  
  1860.     /*
  1861.     * update reply message to add-host requestor
  1862.     */
  1863.  
  1864.     mp2 = wp->wa_mesg;
  1865.     pkint(mp2, count);
  1866.     pkint(mp2, 0);    /* XXX narches = 0 for now */
  1867.     for (i = 0; i < count; i++) {
  1868.         hp = wxp->w_hosts[i];
  1869.         if (hp->hd_err) {
  1870.             pkint(mp2, hp->hd_err);
  1871.             pkstr(mp2, "");
  1872.             pkstr(mp2, "");
  1873.             pkint(mp2, 0);
  1874.             pkint(mp2, 0);
  1875.  
  1876.         } else {
  1877.             pkint(mp2, hp->hd_hostpart);
  1878.             pkstr(mp2, hp->hd_name);
  1879.             pkstr(mp2, hp->hd_arch);
  1880.             pkint(mp2, hp->hd_speed);
  1881.             pkint(mp2, hp->hd_dsig);
  1882.         }
  1883.     }
  1884.  
  1885.     /*
  1886.     * delete broken ones, done now if none succeeded,
  1887.     * otherwise done when host table update is complete.
  1888.     */
  1889.  
  1890.     for (j = i = 0; i < count; i++)
  1891.         if (!wxp->w_hosts[i]->hd_err) {
  1892.             hp = wxp->w_hosts[i];
  1893.             wxp->w_hosts[i] = 0;
  1894.             wxp->w_hosts[j++] = hp;
  1895.  
  1896.         } else {
  1897.             hd_unref(wxp->w_hosts[i]);
  1898.             wxp->w_hosts[i] = 0;
  1899.         }
  1900.     count = j;
  1901.  
  1902.     if (count < 1) {
  1903.         busyadding = 0;
  1904.         sendmessage(wp->wa_mesg);
  1905.         wp->wa_mesg = 0;
  1906.         free_waitc_add(wxp);
  1907.         wait_delete(wp);
  1908.         return 0;
  1909.     }
  1910.  
  1911.     wp2 = wait_new(WT_HTUPD);
  1912.     wp2->wa_dep = wp->wa_dep;
  1913.     wp2->wa_mesg = wp->wa_mesg;
  1914.     wp->wa_mesg = 0;
  1915.  
  1916.     /*
  1917.     * make next host table
  1918.     */
  1919.  
  1920.     newhosts = ht_new(1);
  1921.     newhosts->ht_serial = hosts->ht_serial + 1;
  1922.     newhosts->ht_master = hosts->ht_master;
  1923.     newhosts->ht_cons = hosts->ht_cons;
  1924.     newhosts->ht_local = hosts->ht_local;
  1925.  
  1926.     for (i = 0; i < count; i++)
  1927.         ht_insert(newhosts, wxp->w_hosts[i]);
  1928.  
  1929.     free_waitc_add(wxp);
  1930.     wait_delete(wp);
  1931.     wp = 0;
  1932.  
  1933.     runstate = PVMDHTUPD;
  1934.  
  1935.     /*
  1936.     * send DM_SLCONF message to each new host
  1937.     */
  1938.  
  1939.     for (hh = newhosts->ht_last; hh > 0; hh--)
  1940.         if (hp = newhosts->ht_hosts[hh]) {
  1941.             mp2 = mesg_new(0);
  1942.             mp2->m_tag = DM_SLCONF;
  1943.             mp2->m_dst = hp->hd_hostpart | TIDPVMD;
  1944.             if (hp->hd_epath) {
  1945.                 pkint(mp2, DM_SLCONF_EP);
  1946.                 pkstr(mp2, hp->hd_epath);
  1947.             }
  1948.             if (hp->hd_bpath) {
  1949.                 pkint(mp2, DM_SLCONF_BP);
  1950.                 pkstr(mp2, hp->hd_bpath);
  1951.             }
  1952.             if (hp->hd_wdir) {
  1953.                 pkint(mp2, DM_SLCONF_WD);
  1954.                 pkstr(mp2, hp->hd_wdir);
  1955.             }
  1956.             if (pvmschedtid) {
  1957.                 sprintf(buf2, "%x", pvmschedtid);
  1958.                 pkint(mp2, DM_SLCONF_SCHED);
  1959.                 pkstr(mp2, buf2);
  1960.             }
  1961.             if (pvmtracer.trctid || pvmtracer.outtid) {
  1962.                 sprintf(buf2, "%x %d %d %x %d %d %d %d %s",
  1963.                     pvmtracer.trctid, pvmtracer.trcctx,
  1964.                         pvmtracer.trctag,
  1965.                     pvmtracer.outtid, pvmtracer.outctx,
  1966.                         pvmtracer.outtag,
  1967.                     pvmtracer.trcbuf, pvmtracer.trcopt,
  1968.                     pvmtracer.tmask);
  1969.                 pkint(mp2, DM_SLCONF_TRACE);
  1970.                 pkstr(mp2, buf2);
  1971.             }
  1972.             sendmessage(mp2);
  1973.         }
  1974.  
  1975.     /*
  1976.     * create host table update message containing all current hosts
  1977.     * plus new ones, send to each new host.
  1978.     */
  1979.  
  1980.     mp2 = mesg_new(0);
  1981.     mp2->m_tag = DM_HTUPD;
  1982.     pkint(mp2, newhosts->ht_serial);
  1983.     pkint(mp2, newhosts->ht_master);
  1984.     pkint(mp2, newhosts->ht_cons);
  1985.     pkint(mp2, hosts->ht_cnt + newhosts->ht_cnt);
  1986.     for (hh = hosts->ht_last; hh > 0; hh--)
  1987.         if (hp = hosts->ht_hosts[hh]) {
  1988.             pkint(mp2, hh);
  1989.             pkstr(mp2, hp->hd_name);
  1990.             pkstr(mp2, hp->hd_arch);
  1991.             pkstr(mp2, inadport_hex(&hp->hd_sad));
  1992.             pkint(mp2, hp->hd_mtu);
  1993.             pkint(mp2, hp->hd_speed);
  1994.             pkint(mp2, hp->hd_dsig);
  1995.         }
  1996.     for (hh = newhosts->ht_last; hh > 0; hh--)
  1997.         if (hp = newhosts->ht_hosts[hh]) {
  1998.             pkint(mp2, hh);
  1999.             pkstr(mp2, hp->hd_name);
  2000.             pkstr(mp2, hp->hd_arch);
  2001.             pkstr(mp2, inadport_hex(&hp->hd_sad));
  2002.             pkint(mp2, hp->hd_mtu);
  2003.             pkint(mp2, hp->hd_speed);
  2004.             pkint(mp2, hp->hd_dsig);
  2005.         }
  2006.  
  2007.     for (hh = newhosts->ht_last; hh > 0; hh--)
  2008.         if (hp = newhosts->ht_hosts[hh]) {
  2009.             mp2->m_ref++;
  2010.             mp2->m_dst = hp->hd_hostpart | TIDPVMD;
  2011.             wp3 = wait_new(WT_HTUPD);
  2012.             wp3->wa_dep = wp2->wa_dep;
  2013.             wp2->wa_mesg->m_ref++;
  2014.             wp3->wa_mesg = wp2->wa_mesg;
  2015.             wp3->wa_on = hp->hd_hostpart;
  2016.             LISTPUTBEFORE(wp2, wp3, wa_peer, wa_rpeer);
  2017.             mp2->m_wid = wp3->wa_wid;
  2018.             sendmessage(mp2);
  2019.         }
  2020.     pmsg_unref(mp2);
  2021.  
  2022.     /*
  2023.     * create host table update message containing happy new hosts,
  2024.     * send to each old host.
  2025.     */
  2026.  
  2027.     mp2 = mesg_new(0);
  2028.     mp2->m_tag = DM_HTUPD;
  2029.     pkint(mp2, newhosts->ht_serial);
  2030.     pkint(mp2, newhosts->ht_master);
  2031.     pkint(mp2, newhosts->ht_cons);
  2032.     pkint(mp2, newhosts->ht_cnt);
  2033.     for (hh = newhosts->ht_last; hh > 0; hh--)
  2034.         if (hp = newhosts->ht_hosts[hh]) {
  2035.             pkint(mp2, hh);
  2036.             pkstr(mp2, hp->hd_name);
  2037.             pkstr(mp2, hp->hd_arch);
  2038.             pkstr(mp2, inadport_hex(&hp->hd_sad));
  2039.             pkint(mp2, hp->hd_mtu);
  2040.             pkint(mp2, hp->hd_speed);
  2041.             pkint(mp2, hp->hd_dsig);
  2042.         }
  2043.  
  2044.     for (hh = hosts->ht_last; hh > 0; hh--)
  2045.         if (hh != hosts->ht_local && (hp = hosts->ht_hosts[hh])) {
  2046.             mp2->m_ref++;
  2047.             mp2->m_dst = hp->hd_hostpart | TIDPVMD;
  2048.             wp3 = wait_new(WT_HTUPD);
  2049.             wp3->wa_dep = wp2->wa_dep;
  2050.             wp2->wa_mesg->m_ref++;
  2051.             wp3->wa_mesg = wp2->wa_mesg;
  2052.             wp3->wa_on = hp->hd_hostpart;
  2053.             LISTPUTBEFORE(wp2, wp3, wa_peer, wa_rpeer);
  2054.             mp2->m_wid = wp3->wa_wid;
  2055.             sendmessage(mp2);
  2056.         }
  2057.     pmsg_unref(mp2);
  2058.  
  2059.     /*
  2060.     * update our host table
  2061.     */
  2062.  
  2063.     gotnewhosts(newhosts, hosts);
  2064.  
  2065.     /* XXX returning to normal state right here is a hack, we should
  2066.        XXX wait for all the htupdacks to come back but we need the
  2067.        XXX regular message service, hostfail entry, etc. */
  2068.  
  2069.     for (hh = newhosts->ht_last; hh > 0; hh--)
  2070.         if ((hp = newhosts->ht_hosts[hh]) && !hp->hd_err)
  2071.             ht_insert(hosts, hp);
  2072.     hosts->ht_serial = newhosts->ht_serial;
  2073.  
  2074.     if (pvmdebmask & PDMHOST) {
  2075.         pvmlogerror("startack() committing to new host table:\n");
  2076.         ht_dump(hosts);
  2077.     }
  2078.     runstate = PVMDNORMAL;
  2079.     ht_free(newhosts);
  2080.     newhosts = 0;
  2081.  
  2082.     /* if peered waitcs on htupdack already finished, send the reply */
  2083.  
  2084.     if (wp2->wa_peer == wp2) {
  2085.         busyadding = 0;
  2086.         sendmessage(wp2->wa_mesg);
  2087.         wp2->wa_mesg = 0;
  2088.     }
  2089.     wait_delete(wp2);
  2090.  
  2091.     return 0;
  2092. }
  2093.  
  2094.  
  2095. /*    dm_startack()
  2096. *
  2097. *    The results come back from the pvmd' hoster to pvmd[master].
  2098. *
  2099. *    DM_STARTACK(wid_rtn) {
  2100. *        int count
  2101. *        {
  2102. *            int tid
  2103. *            string result        // line from slave pvmd or error token
  2104. *        } [count]
  2105. *    }
  2106. */
  2107.  
  2108. int
  2109. dm_startack(hp, mp)
  2110.     struct hostd *hp;
  2111.     struct pmsg *mp;
  2112. {
  2113.     struct waitc *wp;        /* wait context on pvmd' */
  2114.  
  2115.     if (!(wp = wait_get(hp, mp, WT_HOSTSTART)))
  2116.         return 0;
  2117.  
  2118.     finack_to_host(hp);
  2119.  
  2120.     startack(wp, mp);
  2121.  
  2122.     return 0;
  2123. }
  2124.  
  2125.  
  2126. /*    dm_task()
  2127. *
  2128. *    (Remote or local) pvmd requests information about local task(s).
  2129. *
  2130. *    DM_TASK(wid) {
  2131. *        int where            // 0 for all, hostpart, or full tid
  2132. *    }
  2133. */
  2134.  
  2135. int
  2136. dm_task(hp, mp)
  2137.     struct hostd *hp;
  2138.     struct pmsg *mp;
  2139. {
  2140.     struct task *tp;
  2141.     struct pmsg *mp2;
  2142.     int where;
  2143.  
  2144.     hp = hp;
  2145.  
  2146.     if (upkuint(mp, &where)) {
  2147.         pvmlogerror("dm_task() bad msg format\n");
  2148.         return 0;
  2149.     }
  2150.  
  2151. #ifdef SHMEM
  2152.     mpp_setstatus(0);    /* get status from global info table */
  2153. #endif
  2154.  
  2155.     /* pack list of local tasks and reply to waiter */
  2156.  
  2157.     mp2 = mesg_new(0);
  2158.     mp2->m_dst = mp->m_src;
  2159.     mp2->m_tag = DM_TASKACK;
  2160.     mp2->m_wid = mp->m_wid;
  2161.     if (where & tidlmask) {
  2162.         if (tp = task_find(where)) {
  2163.             pkint(mp2, tp->t_tid);
  2164.             pkint(mp2, tp->t_ptid);
  2165.             pkint(mp2, myhostpart);
  2166.             pkint(mp2, tp->t_flag);
  2167.             pkstr(mp2, tp->t_a_out ? tp->t_a_out : "");
  2168.             pkint(mp2, tp->t_pid);
  2169.         }
  2170.  
  2171.     } else {
  2172.         for (tp = locltasks->t_link; tp != locltasks; tp = tp->t_link) {
  2173.             pkint(mp2, tp->t_tid);
  2174.             pkint(mp2, tp->t_ptid);
  2175.             pkint(mp2, myhostpart);
  2176.             pkint(mp2, tp->t_flag);
  2177.             pkstr(mp2, (tp->t_a_out ? tp->t_a_out : ""));
  2178.             pkint(mp2, tp->t_pid);
  2179.         }
  2180.     }
  2181.     sendmessage(mp2);
  2182.     return 0;
  2183. }
  2184.  
  2185.  
  2186. /*    dm_taskack()
  2187. *
  2188. *    Reply to DM_TASK op.
  2189. *
  2190. *    DM_TASKACK(wid_rtn) {
  2191. *        {
  2192. *            int tid
  2193. *            int ptid
  2194. *            int hostpart
  2195. *            int flag
  2196. *            string a_out
  2197. *            int pid
  2198. *        } []                // implied
  2199. *    }
  2200. */
  2201.  
  2202. int
  2203. dm_taskack(hp, mp)
  2204.     struct hostd *hp;
  2205.     struct pmsg *mp;
  2206. {
  2207.     struct waitc *wp;
  2208.     struct pmsg *mp2;
  2209.     int i;
  2210.     char *p;
  2211.  
  2212.     if (!(wp = wait_get(hp, mp, WT_TASK)))
  2213.         return 0;
  2214.  
  2215.     /* append data to waiting message */
  2216.  
  2217.     mp2 = wp->wa_mesg;
  2218.     while (!upkint(mp, &i)) {
  2219.         pkint(mp2, i);            /* tid */
  2220.         upkint(mp, &i);            /* ptid */
  2221.         pkint(mp2, i);
  2222.         upkint(mp, &i);            /* host */
  2223.         pkint(mp2, i);
  2224.         upkint(mp, &i);            /* flag */
  2225.         pkint(mp2, i);
  2226.         upkstralloc(mp, &p);    /* a.out name */
  2227.         pkstr(mp2, p);
  2228.         PVM_FREE(p);
  2229.         upkint(mp, &i);            /* pid */
  2230.         pkint(mp2, i);
  2231.     }
  2232.  
  2233.     /* send message if we're the last waiter */
  2234.  
  2235.     if (wp->wa_peer == wp) {
  2236.         mp2->m_ref++;
  2237.         sendmessage(mp2);
  2238.  
  2239.     }
  2240.     wait_delete(wp);
  2241.     return 0;
  2242. }
  2243.  
  2244.  
  2245. /*    dm_delhost()
  2246. *
  2247. *    (Master pvmd) gets request to delete hosts.
  2248. *
  2249. *    DM_DELHOST(wid) {
  2250. *        int count
  2251. *        string names[count]
  2252. *    }
  2253. */
  2254.  
  2255. int
  2256. dm_delhost(hp, mp)
  2257.     struct hostd *hp;
  2258.     struct pmsg *mp;
  2259. {
  2260.     int count;
  2261.     char *buf;
  2262.     struct pmsg *mp2;        /* DELHOSTACK message */
  2263.     struct pmsg *mp3;        /* HTDEL message */
  2264.     struct htab *ht_del;    /* hosts to delete */
  2265.     struct htab *ht_save;    /* remaining hosts */
  2266.     int hh;
  2267.  
  2268. /* XXX danger, this doesn't check if already doing a host add/delete */
  2269.  
  2270.     /* sanity check count */
  2271.  
  2272.     if (upkint(mp, &count)) {
  2273.         pvmlogerror("dm_delhost() bad msg format\n");
  2274.         return 0;
  2275.     }
  2276.     if (count < 1 || count > (tidhmask >> (ffs(tidhmask) - 1))) {
  2277.         pvmlogerror("dm_delhost() bad count\n");
  2278.         return 0;
  2279.     }
  2280.  
  2281.     /*
  2282.     * read host names from message, generate delete and save sets
  2283.     * and a DELHOSTACK reply message with result code for each host.
  2284.     * set SHUTDOWN flag for each host in delete set.
  2285.     */
  2286.  
  2287.     ht_del = ht_new(1);
  2288.     ht_save = ht_new(1);
  2289.     ht_merge(ht_save, hosts);
  2290.  
  2291.     mp2 = mesg_new(0);
  2292.     mp2->m_tag = DM_DELHOSTACK;
  2293.     mp2->m_wid = mp->m_wid;
  2294.     mp2->m_dst = mp->m_src;
  2295.  
  2296.     mp3 = mesg_new(0);
  2297.     mp3->m_tag = DM_HTDEL;
  2298.     pkint(mp3, hosts->ht_serial);
  2299.  
  2300.     pkint(mp2, count);
  2301.     while (count-- > 0) {
  2302.         upkstralloc(mp, &buf);
  2303.         if (hp = nametohost(hosts, buf)) {
  2304.             if (tidtohost(ht_del, hp->hd_hostpart)) {
  2305.                 pkint(mp2, PvmDupHost);
  2306.  
  2307.             } else {
  2308.                 if (hp->hd_hostpart == myhostpart)
  2309.                     pkint(mp2, PvmBadParam);
  2310.  
  2311.                 else {
  2312.                     ht_insert(ht_del, hp);
  2313.                     ht_delete(ht_save, hp);
  2314.                     pkint(mp3, hp->hd_hostpart);
  2315.                     fin_to_host(hp);
  2316.                     pkint(mp2, 0);
  2317.                 }
  2318.             }
  2319.  
  2320.         } else
  2321.             pkint(mp2, PvmNoHost);
  2322.         PVM_FREE(buf);
  2323.     }
  2324.  
  2325.     /*
  2326.     * send HTDEL message to all hosts in ht_save set except us
  2327.     */
  2328.  
  2329.     for (hh = ht_save->ht_last; hh > 0; hh--)
  2330.         if (hh != hosts->ht_local && (hp = ht_save->ht_hosts[hh])) {
  2331.             mp3->m_ref++;
  2332.             mp3->m_dst = hp->hd_hostpart | TIDPVMD;
  2333.             sendmessage(mp3);
  2334.         }
  2335.     pmsg_unref(mp3);
  2336.  
  2337.     /* reply to host that requested DELHOST operation */
  2338.  
  2339.     sendmessage(mp2);
  2340.  
  2341.     ht_free(ht_del);
  2342.     ht_free(ht_save);
  2343.     return 0;
  2344. }
  2345.  
  2346.  
  2347. /*    dm_delhostack()
  2348. *
  2349. *    Reply to DM_DELHOST operation.
  2350. *
  2351. *    DM_DELHOSTACK(wid_rtn) {
  2352. *        int count            // or negative for error
  2353. *        int status[count]    // status of each host
  2354. *    }
  2355. */
  2356.  
  2357. int
  2358. dm_delhostack(hp, mp)
  2359.     struct hostd *hp;
  2360.     struct pmsg *mp;
  2361. {
  2362.     struct waitc *wp;
  2363.  
  2364.     if (!(wp = wait_get(hp, mp, WT_DELHOST)))
  2365.         return 0;
  2366.  
  2367.     pmsg_packbody(wp->wa_mesg, mp);
  2368.     sendmessage(wp->wa_mesg);
  2369.     wp->wa_mesg = 0;
  2370.     wait_delete(wp);
  2371.     return 0;
  2372. }
  2373.  
  2374.  
  2375. /*    dm_null()
  2376. *
  2377. *    No-op message.
  2378. *
  2379. *    DM_NULL { }
  2380. */
  2381.  
  2382. int
  2383. dm_null(hp, mp)
  2384.     struct hostd *hp;
  2385.     struct pmsg *mp;
  2386. {
  2387.     hp = hp;
  2388.     mp = mp;
  2389. /*
  2390.     pvmlogprintf("dm_null() from %s\n", hp->hd_name);
  2391. */
  2392.     return 0;
  2393. }
  2394.  
  2395.  
  2396. /*    dm_taskout()
  2397. *
  2398. *    Stdout data from a task.
  2399. *
  2400. *    DM_TASKOUT {
  2401. *        int tid
  2402. *        int length
  2403. *        char data[length]
  2404. *    }
  2405. */
  2406.  
  2407. int
  2408. dm_taskout(hp, mp)
  2409.     struct hostd *hp;
  2410.     struct pmsg *mp;
  2411. {
  2412.     int tid;
  2413.     int l;
  2414.     char *p, *q, c;
  2415.     char buf2[32];
  2416.     char buf[4100];         /* XXX a bit bigger than in pvmd.c`loclstout() */
  2417.  
  2418.     hp = hp;
  2419.  
  2420.     if (upkuint(mp, &tid) || upkint(mp, &l) || l < 1)
  2421.         return 0;
  2422.  
  2423.     /* unpack data, leaving room at head of buffer */
  2424.  
  2425.     p = buf + 32;
  2426.     if (l > sizeof(buf) - (p - buf) - 2)
  2427.         l = sizeof(buf) - (p - buf) - 2;
  2428.     upkbyte(mp, p, l);
  2429.  
  2430.     /* ends with "\n\0" */
  2431.     if (p[l - 1] != '\n')
  2432.         p[l++] = '\n';
  2433.     p[l] = 0;
  2434.  
  2435.     sprintf(buf2, "[t%x] ", tid);
  2436.     l = strlen(buf2);
  2437.     while (*p) {
  2438.         for (q = p; *q++ != '\n'; ) ;
  2439.         c = *q;
  2440.         *q = 0;
  2441.         BCOPY(buf2, p - l, l);
  2442.         pvmlogerror(p - l);
  2443.         *q = c;
  2444.         p = q;
  2445.     }
  2446.     return 0;
  2447. }
  2448.  
  2449.  
  2450. /*    dm_pstat()
  2451. *
  2452. *    (Remote or local) pvmd requests status of a local task.
  2453. *
  2454. *    DM_PSTAT(wid) {
  2455. *        int tid
  2456. *    }
  2457. */
  2458.  
  2459. int
  2460. dm_pstat(hp, mp)
  2461.     struct hostd *hp;
  2462.     struct pmsg *mp;
  2463. {
  2464.     int tid;
  2465.     struct pmsg *mp2;
  2466.  
  2467.     hp = hp;
  2468.     upkuint(mp, &tid);
  2469.     if (tid == pvmmytid || task_find(tid))
  2470.         tid = 0;
  2471.     else
  2472.         tid = PvmNoTask;
  2473.     mp2 = mesg_new(0);
  2474.     mp2->m_dst = mp->m_src;
  2475.     mp2->m_tag = DM_PSTATACK;
  2476.     mp2->m_wid = mp->m_wid;
  2477.     pkint(mp2, tid);
  2478.     sendmessage(mp2);
  2479.     return 0;
  2480. }
  2481.  
  2482.  
  2483. /*    dm_pstatack()
  2484. *
  2485. *    Reply to DM_PSTAT op.
  2486. *
  2487. *    DM_PSTATACK(wid_rtn) {
  2488. *        int status
  2489. *    }
  2490. */
  2491.  
  2492. int
  2493. dm_pstatack(hp, mp)
  2494.     struct hostd *hp;
  2495.     struct pmsg *mp;
  2496. {
  2497.     struct waitc *wp;
  2498.  
  2499.     if (!(wp = wait_get(hp, mp, 0)))
  2500.         return 0;
  2501.  
  2502.     pmsg_packbody(wp->wa_mesg, mp);
  2503. pmsg_dump(mp, 2);
  2504. pmsg_dump(wp->wa_mesg, 2);
  2505.     sendmessage(wp->wa_mesg);
  2506.     wp->wa_mesg = 0;
  2507.     wait_delete(wp);
  2508.     return 0;
  2509. }
  2510.  
  2511.  
  2512. /*    dm_halt()
  2513. *
  2514. *    Task has requested whole machine to halt.
  2515. *
  2516. *    DM_HALT { }
  2517. */
  2518.  
  2519. int
  2520. dm_halt(hp, mp)
  2521.     struct hostd *hp;
  2522.     struct pmsg *mp;
  2523. {
  2524.     int hh;
  2525.  
  2526.     pvmlogprintf("dm_halt() from (%s), halting...\n", hp->hd_name);
  2527.     for (hh = hosts->ht_last; hh > 0; hh--) {
  2528.         if (hh == hosts->ht_local || !(hp = hosts->ht_hosts[hh]))
  2529.             continue;
  2530.         finack_to_host(hp);
  2531.     }
  2532.     runstate = PVMDHALTING;
  2533.     return 0;
  2534. }
  2535.  
  2536.  
  2537. /*    dm_mca()
  2538. *
  2539. *    Remote pvmd defines a new (single-use) multicast address, to be used
  2540. *    by a subsequent message.
  2541. *
  2542. *    DM_MCA {
  2543. *        int gtid            // multicast tid
  2544. *        int count            // number of addresses
  2545. *        int tids[count]        // addresses
  2546. *    }
  2547. */
  2548.  
  2549. int
  2550. dm_mca(hp, mp)
  2551.     struct hostd *hp;
  2552.     struct pmsg *mp;
  2553. {
  2554.     struct mca *mcap;
  2555.     int i;
  2556.  
  2557.     /* unpack struct mca from message */
  2558.  
  2559.     mcap = mca_new();
  2560.     upkuint(mp, &mcap->mc_tid);
  2561.     upkint(mp, &mcap->mc_ndst);
  2562.     mcap->mc_dsts = TALLOC(mcap->mc_ndst, int, "mcad");
  2563.     for (i = 0; i < mcap->mc_ndst; i++)
  2564.         upkuint(mp, &mcap->mc_dsts[i]);
  2565.  
  2566.     /* put on list of mcas of src host */
  2567.  
  2568.     LISTPUTBEFORE(hp->hd_mcas, mcap, mc_link, mc_rlink);
  2569.  
  2570.     if (pvmdebmask & PDMMESSAGE) {
  2571.         pvmlogprintf("dm_mca() mca %x from %s\n", mcap->mc_tid, hp->hd_name);
  2572.     }
  2573.     return 0;
  2574. }
  2575.  
  2576.  
  2577. /*    dm_notify()
  2578. *
  2579. *    Remote pvmd requests to be notified on an event.
  2580. *
  2581. *    DM_NOTIFY(wid) {
  2582. *        int what            // event type, currenty only PvmTaskExit
  2583. *        int tid                // address
  2584. *    }
  2585. */
  2586.  
  2587. int
  2588. dm_notify(hp, mp)
  2589.     struct hostd *hp;
  2590.     struct pmsg *mp;
  2591. {
  2592.     int what, tid;
  2593.     struct waitc *wp;
  2594.     struct pmsg *mp2;
  2595.  
  2596.     hp = hp;
  2597.  
  2598.     upkint(mp, &what);
  2599.     upkuint(mp, &tid);
  2600.     if (what != PvmTaskExit) {
  2601.         pvmlogprintf("dm_notify() what = %d?\n", what);
  2602.         return 0;
  2603.     }
  2604.  
  2605.     mp2 = mesg_new(0);
  2606.     mp2->m_dst = mp->m_src;
  2607.     mp2->m_tag = DM_NOTIFYACK;
  2608.     mp2->m_wid = mp->m_wid;
  2609.     pkint(mp2, tid);
  2610.  
  2611.     if (task_find(tid)) {
  2612.         wp = wait_new(WT_TASKX);
  2613.         wp->wa_on = tid;
  2614.         wp->wa_tid = mp->m_src;
  2615.         wp->wa_dep = mp->m_wid;
  2616.         wp->wa_mesg = mp2;
  2617.  
  2618.     } else {
  2619.         sendmessage(mp2);
  2620.     }
  2621.     return 0;
  2622. }
  2623.  
  2624.  
  2625. /*    dm_notifyack()
  2626. *
  2627. *    Reply to DM_NOTIFY op.
  2628. *
  2629. *    DM_NOTIFYACK(wid_rtn) { }
  2630. */
  2631.  
  2632. int
  2633. dm_notifyack(hp, mp)
  2634.     struct hostd *hp;
  2635.     struct pmsg *mp;
  2636. {
  2637.     struct waitc *wp;
  2638.  
  2639.     hp = hp;
  2640.  
  2641.     if (!(wp = wait_get((struct hostd*)0, mp, 0)))
  2642.         return 0;
  2643.  
  2644.     if (wp->wa_tid && wp->wa_mesg) {
  2645.         sendmessage(wp->wa_mesg);
  2646.         wp->wa_mesg = 0;
  2647.     }
  2648.  
  2649.     if ( wp->wa_tid == pvmmytid )
  2650.         mb_tidy(wp->wa_on);
  2651.  
  2652.     wait_delete(wp);
  2653.     return 0;
  2654. }
  2655.  
  2656.  
  2657. /*    dm_db()
  2658. *
  2659. *    (Remote or local) pvmd requests a database op.
  2660. *
  2661. *    DM_DB(wid) {
  2662. *        int opcode        // TMDB_PUT, TMDB_REMOVE, TMDB_GET, TMDB_NAMES
  2663. *        int tid            // owner task (XXX we're just taking its word, hahaha)
  2664. *        string name
  2665. *        int index
  2666. *        int flags
  2667. *        msg data        // if TMDB_PUT
  2668. *    }
  2669. */
  2670.  
  2671. int
  2672. dm_db(hp, mp)
  2673.     struct hostd *hp;
  2674.     struct pmsg *mp;
  2675. {
  2676.     int opcode;                    /* op requested */
  2677.     int tid;
  2678.     int req;                    /* index requested */
  2679.     int flags;
  2680.     char *name = 0;                /* class name */
  2681.     struct pmsg *mp2 = 0;        /* reply */
  2682.     struct pmsg *mp3 = 0;        /* data message */
  2683.  
  2684.     struct waitc *wp, *wp2;        /* wait ctx ptrs (notify, recvinfo */
  2685.     struct pmsg *mp4 = 0;        /* notify forward message */
  2686.     struct hostd *hp2;            /* remote notify host */
  2687.  
  2688.     struct pvmmclass *np, *np2;    /* reset pointers */
  2689.     struct pvmmentry *ep, *ep2;    /* reset pointers */
  2690.     int *noresets;                /* noreset tids */
  2691.     int nnr;                    /* # of noreset tasks */
  2692.     int found;
  2693.     int cc;
  2694.     int i;
  2695.  
  2696.     hp = hp;
  2697.  
  2698.     if (upkint(mp, &opcode) || upkint(mp, &tid)
  2699.     || upkstralloc(mp, &name) || upkint(mp, &req) || upkint(mp, &flags))
  2700.         goto badformat;
  2701.  
  2702.     mp2 = mesg_new(0);
  2703.     mp2->m_dst = mp->m_src;
  2704.     mp2->m_tag = DM_DBACK;
  2705.     mp2->m_wid = mp->m_wid;
  2706.  
  2707.     switch (opcode) {
  2708.  
  2709.     case TMDB_PUT:
  2710.         mp3 = mesg_new(0);
  2711.         if (pmsg_unpack(mp, mp3))
  2712.             goto badformat;
  2713.         if ((req = mb_insert(tid, name, req, flags, mp3)) < 0)
  2714.             pmsg_unref(mp3);
  2715.         else {
  2716.             /* dummy notify for clean up */
  2717.             wp = wait_new(WT_TASKX);
  2718.             wp->wa_on = tid;
  2719.             wp->wa_tid = pvmmytid;
  2720.             wp->wa_dep = 0;
  2721.             wp->wa_mesg = (struct pmsg *) NULL;
  2722.  
  2723.             /* pass on to non-master host */
  2724.             hp2 = tidtohost(hosts, tid);
  2725.             if ( hp2 && hp2->hd_hostpart != myhostpart ) {
  2726.                 mp4 = mesg_new(0);
  2727.                 pkint(mp4, PvmTaskExit);
  2728.                 pkint(mp4, tid);
  2729.                 mp4->m_dst = hp2->hd_hostpart | TIDPVMD;
  2730.                 mp4->m_tag = DM_NOTIFY;
  2731.                 mp4->m_wid = wp->wa_wid;
  2732.                 sendmessage(mp4);
  2733.             }
  2734.  
  2735.             /* check for any pending requests for this mbox entry */
  2736.             for (wp = waitlist->wa_link; wp != waitlist; wp = wp2) {
  2737.                 wp2 = wp->wa_link;
  2738.                 if (wp->wa_kind == WT_RECVINFO) {
  2739.                     ep = (struct pvmmentry *) wp->wa_spec;
  2740.                     if ( !strcmp( (char *) ep->me_msg, name ) ) {
  2741.                         cc = mb_lookup(ep->me_tid, (char *) ep->me_msg,
  2742.                             ep->me_ind, ep->me_flags, &mp3);
  2743.                         if ( cc != PvmNotFound ) {
  2744.                             pkint(wp->wa_mesg, cc);
  2745.                             if (mp3) {
  2746.                                 pmsg_pack(wp->wa_mesg, mp3);
  2747.                                 pmsg_unref(mp3);
  2748.                             }
  2749.                             sendmessage(wp->wa_mesg);
  2750.                             PVM_FREE(ep->me_msg);
  2751.                             PVM_FREE(ep);
  2752.                             wait_delete(wp);
  2753.                         }
  2754.                     }
  2755.                 }
  2756.             }
  2757.         }
  2758.         pkint(mp2, req);
  2759.         break;
  2760.  
  2761.     case TMDB_REMOVE:
  2762.         req = mb_delete(tid, name, req, flags);
  2763.         pkint(mp2, req);
  2764.         break;
  2765.  
  2766.     case TMDB_GET:
  2767.         cc = mb_lookup(tid, name, req, flags, &mp3);
  2768.         if ( cc == PvmNotFound && (flags & PvmMboxWaitForInfo) ) {
  2769.             ep = me_new(req);
  2770.             ep->me_tid = tid;
  2771.             ep->me_msg = (struct pmsg *) name;  /* XXX ouch, overload */
  2772.             ep->me_flags = flags;
  2773.  
  2774.             wp = wait_new(WT_RECVINFO);
  2775.             wp->wa_on = tid;
  2776.             wp->wa_tid = pvmmytid;
  2777.             wp->wa_dep = mp->m_wid;
  2778.             wp->wa_mesg = mp2;
  2779.             wp->wa_spec = (void *) ep;
  2780.  
  2781.             return 0;
  2782.         } else {
  2783.             pkint(mp2, cc);
  2784.             if (mp3) {
  2785.                 pmsg_pack(mp2, mp3);
  2786.                 pmsg_unref(mp3);
  2787.             }
  2788.         }
  2789.         break;
  2790.  
  2791.     case TMDB_NAMES:
  2792.         pkint(mp2, 0);
  2793.         req = mb_names(tid, name, mp2);
  2794.         break;
  2795.  
  2796.     case TMDB_RESET:
  2797.         if ( upkint(mp, &nnr) )
  2798.             goto badformat;
  2799.         noresets = TALLOC( nnr, int, "int" );
  2800.         for ( i=0 ; i < nnr ; i++ ) {
  2801.             if ( upkint(mp, &(noresets[i])) ) {
  2802.                 PVM_FREE(noresets);
  2803.                 goto badformat;
  2804.             }
  2805.         }
  2806.         pkint(mp2, 0);
  2807.         for (np = pvmmboxclasses->mc_link; np != pvmmboxclasses;
  2808.                 np = np2) {
  2809.             np2 = np->mc_link;
  2810.  
  2811.             /* If name passed in, only wipe mboxes in that class */
  2812.             if ( *name == '\0' || !strcmp( np->mc_name, name ) ) {
  2813.  
  2814.             for (ep = np->mc_ent->me_link; ep != np->mc_ent; ep = ep2) {
  2815.                 ep2 = ep->me_link;
  2816.  
  2817.                 /* If index passed in, only wipe that mbox */
  2818.                 /* -1 == All Entries */
  2819.                 if ( req < 0 || req == ep->me_ind ) {
  2820.  
  2821.                 /* Check for Persistency (that thorn in my "side" :-) */
  2822.                 if ( ep->me_flags & PvmMboxPersistent ) {
  2823.  
  2824.                     /* Task Already Gone?  Spank It. */
  2825.                     if (!(ep->me_tid)) {
  2826.                         me_free(np, ep);
  2827.                         if (np2->mc_rlink != np)
  2828.                             break;
  2829.                     }
  2830.                 
  2831.                     /* Check for No-Reset Task */
  2832.                     /* (Only if actually killing tasks...) */
  2833.                     else if ( flags ) {  /* killtasks */
  2834.                         for ( i=0, found=0 ; i < nnr && !found ; i++ )
  2835.                             if ( noresets[i] == ep->me_tid )
  2836.                                 found++;
  2837.  
  2838.                         /* Not a No-Reset, It WILL die soon. */
  2839.                         /* Wait for cleanup. */
  2840.                         if ( !found ) {
  2841.                             wp = wait_new(WT_RESET);
  2842.                             wp->wa_on = ep->me_tid;
  2843.                             wp->wa_tid = pvmmytid;
  2844.                             wp->wa_dep = 0;
  2845.                             wp->wa_mesg = (struct pmsg *) NULL;
  2846.  
  2847.                             /* pass on to non-master host */
  2848.                             hp2 = tidtohost(hosts, ep->me_tid);
  2849.                             if ( hp2 && hp2->hd_hostpart != myhostpart )
  2850.                             {
  2851.                                 mp4 = mesg_new(0);
  2852.                                 pkint(mp4, ep->me_tid);
  2853.                                 mp4->m_dst = hp2->hd_hostpart | TIDPVMD;
  2854.                                 mp4->m_tag = DM_RESET;
  2855.                                 mp4->m_wid = wp->wa_wid;
  2856.                                 sendmessage(mp4);
  2857.                             }
  2858.                         }
  2859.                     }
  2860.                 }
  2861.                 }
  2862.             }
  2863.             }
  2864.         }
  2865.         PVM_FREE(noresets);
  2866.         break;
  2867.  
  2868.     default:
  2869.         goto badformat;
  2870.     }
  2871.  
  2872.     sendmessage(mp2);
  2873.     PVM_FREE(name);
  2874.     return 0;
  2875.  
  2876. badformat:
  2877.     pvmlogerror("dm_db() bad msg format\n");
  2878.  
  2879.     if (name)
  2880.         PVM_FREE(name);
  2881.     if (mp2)
  2882.         pmsg_unref(mp2);
  2883.     return 0;
  2884. }
  2885.  
  2886.  
  2887. /*    dm_dback()
  2888. *
  2889. *    Reply to DM_DB op.
  2890. *
  2891. *    DM_DBACK(wid_rtn) {
  2892. *        int status        // and index
  2893. *    if TMDB_PUT, TMDB_REMOVE, TMDB_GET
  2894. *        msg data        // if TMDB_GET
  2895. *    else if TMDB_NAMES
  2896. *        string names[]    // list of names, length implied
  2897. *    }
  2898. */
  2899.  
  2900. int
  2901. dm_dback(hp, mp)
  2902.     struct hostd *hp;
  2903.     struct pmsg *mp;
  2904. {
  2905.     struct waitc *wp;
  2906.  
  2907.     if (!(wp = wait_get(hp, mp, WT_DB)))
  2908.         return 0;
  2909.  
  2910.     pmsg_packbody(wp->wa_mesg, mp);
  2911.     sendmessage(wp->wa_mesg);
  2912.     wp->wa_mesg = 0;
  2913.     wait_delete(wp);
  2914.     return 0;
  2915. }
  2916.  
  2917.  
  2918. /*    dm_reset()
  2919. *
  2920. *    Remote pvmd requests to be notified when task is reset.
  2921. *        - for mbox cleanup...
  2922. *
  2923. *    DM_RESET(wid) {
  2924. *        int tid                // task id of persistent mbox owner
  2925. *    }
  2926. */
  2927.  
  2928. int
  2929. dm_reset(hp, mp)
  2930.     struct hostd *hp;
  2931.     struct pmsg *mp;
  2932. {
  2933.     int tid;
  2934.     struct waitc *wp;
  2935.     struct pmsg *mp2;
  2936.  
  2937.     hp = hp;
  2938.  
  2939.     upkuint(mp, &tid);
  2940.  
  2941.     mp2 = mesg_new(0);
  2942.     mp2->m_dst = mp->m_src;
  2943.     mp2->m_tag = DM_RESETACK;
  2944.     mp2->m_wid = mp->m_wid;
  2945.     pkint(mp2, tid);
  2946.  
  2947.     if (task_find(tid)) {
  2948.         wp = wait_new(WT_RESET);
  2949.         wp->wa_on = tid;
  2950.         wp->wa_tid = mp->m_src;
  2951.         wp->wa_dep = mp->m_wid;
  2952.         wp->wa_mesg = mp2;
  2953.  
  2954.     } else {
  2955.         sendmessage(mp2);
  2956.     }
  2957.  
  2958.     return 0;
  2959. }
  2960.  
  2961.  
  2962. /*    dm_resetack()
  2963. *
  2964. *    Reply to DM_RESET op.
  2965. *
  2966. *    DM_RESETACK(wid_rtn) { }
  2967. */
  2968.  
  2969. int
  2970. dm_resetack(hp, mp)
  2971.     struct hostd *hp;
  2972.     struct pmsg *mp;
  2973. {
  2974.     struct waitc *wp;
  2975.  
  2976.     hp = hp;
  2977.  
  2978.     if (!(wp = wait_get((struct hostd*)0, mp, 0)))
  2979.         return 0;
  2980.  
  2981.     if (wp->wa_tid && wp->wa_mesg) {
  2982.         sendmessage(wp->wa_mesg);
  2983.         wp->wa_mesg = 0;
  2984.     }
  2985.  
  2986.     mb_tidy_reset(wp->wa_on);
  2987.  
  2988.     wait_delete(wp);
  2989.     return 0;
  2990. }
  2991.  
  2992.  
  2993. int
  2994. dm_htdel(hp, mp)
  2995.     struct hostd *hp;
  2996.     struct pmsg *mp;
  2997. {
  2998.     int serial;
  2999.     int tid;
  3000.  
  3001.     if (hp != hosts->ht_hosts[hosts->ht_master]) {
  3002.         pvmlogprintf("dm_htdel() from t%x (not master)?\n", mp->m_src);
  3003.         return 0;
  3004.     }
  3005.     if (upkint(mp, &serial)) {
  3006.         pvmlogerror("dm_htdel() bad format\n");
  3007.         return 0;
  3008.     }
  3009.     if (serial != hosts->ht_serial) {
  3010.         pvmlogprintf("dm_htdel() for serial %d, current is %d?\n",
  3011.                 serial, hosts->ht_serial);
  3012.         return 0;
  3013.     }
  3014.     while (!upkuint(mp, &tid)) {
  3015.         if (hp = tidtohost(hosts, tid)) {
  3016.             if (pvmdebmask & PDMHOST) {
  3017.                 pvmlogprintf("dm_htdel() host %s\n", hp->hd_name);
  3018.             }
  3019.             hostfailentry(hp);
  3020.             ht_delete(hosts, hp);
  3021.             if (newhosts)
  3022.                 ht_delete(newhosts, hp);
  3023.         }
  3024.     }
  3025.     return 0;
  3026. }
  3027.  
  3028.  
  3029. /*    dm_hostsync()
  3030. *
  3031. *    Request time of day clock sample
  3032. *
  3033. *    DM_HOSTSYNC(wid) { }
  3034. */
  3035.  
  3036. int
  3037. dm_hostsync(hp, mp)
  3038.     struct hostd *hp;
  3039.     struct pmsg *mp;
  3040. {
  3041.     struct pmsg *mp2;
  3042.     struct timeval now;
  3043.  
  3044.     mp = mp;
  3045.     hp = hp;
  3046.  
  3047.     mp2 = mesg_new(0);
  3048.     mp2->m_dst = mp->m_src;
  3049.     mp2->m_tag = DM_HOSTSYNCACK;
  3050.     mp2->m_wid = mp->m_wid;
  3051.     gettimeofday(&now, (struct timezone *)0);
  3052.     pkint(mp2, (int)now.tv_sec);
  3053.     pkint(mp2, (int)now.tv_usec);
  3054.     sendmessage(mp2);
  3055.     return 0;
  3056. }
  3057.  
  3058.  
  3059. /*    dm_hostsyncack()
  3060. *
  3061. *    Clock sample comes back
  3062. *
  3063. *    DM_HOSTSYNCACK(wid_rtn) {
  3064. *        int sec
  3065. *        int usec
  3066. *    }
  3067. */
  3068.  
  3069. int
  3070. dm_hostsyncack(hp, mp)
  3071.     struct hostd *hp;
  3072.     struct pmsg *mp;
  3073. {
  3074.     struct waitc *wp;
  3075.     int i;
  3076.  
  3077.     if (!(wp = wait_get(hp, mp, WT_HOSTSYNC)))
  3078.         return 0;
  3079.  
  3080.     pkint(wp->wa_mesg, 0);
  3081.     upkuint(mp, &i);
  3082.     pkint(wp->wa_mesg, i);
  3083.     upkuint(mp, &i);
  3084.     pkint(wp->wa_mesg, i);
  3085.     sendmessage(wp->wa_mesg);
  3086.     wp->wa_mesg = 0;
  3087.     wait_delete(wp);
  3088.     return 0;
  3089. }
  3090.  
  3091.  
  3092. struct mca *
  3093. mca_new()
  3094. {
  3095.     struct mca *mcap;
  3096.  
  3097.     if (mcap = TALLOC(1, struct mca, "mca")) {
  3098.         mcap->mc_link = mcap->mc_rlink = mcap;
  3099.         mcap->mc_tid = mcap->mc_ndst = 0;
  3100.         mcap->mc_dsts = 0;
  3101.     }
  3102.     return mcap;
  3103. }
  3104.  
  3105.  
  3106. void
  3107. mca_free(mcap)
  3108.     struct mca *mcap;
  3109. {
  3110.     LISTDELETE(mcap, mc_link, mc_rlink);
  3111.     if (mcap->mc_dsts)
  3112.         PVM_FREE(mcap->mc_dsts);
  3113.     PVM_FREE(mcap);
  3114. }
  3115.  
  3116.  
  3117. struct pmsg *
  3118. mesg_new(master)
  3119.     int master;
  3120. {
  3121.     struct pmsg *mp;
  3122.  
  3123.     if (mp = pmsg_new(master)) {
  3124.         mp->m_src = pvmmytid;
  3125.         pmsg_setenc(mp, 0x10000000);    /* PvmDataDefault */
  3126.         (mp->m_codef->enc_init)(mp);
  3127.     }
  3128.     return mp;
  3129. }
  3130.  
  3131.  
  3132.